开发者

C++ boost template parameter traits

开发者 https://www.devze.com 2023-01-06 01:25 出处:网络
I believe I had seen macro in boost that recovers template template parameters, for example: template<class>

I believe I had seen macro in boost that recovers template template parameters, for example:

template<class>
struct parameters;

#de开发者_如何学Pythonfine parameters(T) template<class A> \
          struct parameters<T<A> > { typedef A type1; };

is there one like this, or am I wrong?

Thank you


delctype support in C++0x makes this fairly trivial to implement:

template<template <typename> class Parent, typename Param1>
Param1 get_type(Parent<Param1> const &input) { return Param1(); }

SomeTpl<int> some_obj;
delctype(get_type(some_obj)) x;

(Though you need a separate get_type definition for templates with 2, 3, 4, etc parameters.)

Unfortunately, I don't think there is a way to do this without decltype, because to do so required automatic the type-deduction provided by function templates (which is not available for class templates) and so there's no way to make a typedef that way.

I don't know off-hand if boost has anything like this already, but if they do it will still require your compiler to support decltype, but since decltype is so new there is not a lot of stuff in boost that uses it yet (though there is some).


I have learned to trust Johannes' statements, so I'm a bit confused, since this seems to compile Ok for me with VC10 and prints the expected int:

#include <iostream>
#include <typeinfo>

template< class T >
class steal_it;

template< typename U, template<typename> class C >
struct steal_it< C<U> > { 
    typedef U result_t;
};

template< typename T >
class foo {};

template< typename T >
void test_it(T)
{
    typename steal_it<T>::result_t bar = 42;
    std::cout << typeid(bar).name() << '\n';
}

int main(){

    test_it( foo<int>() );

    return 0;
}

Of course, since I didn't check with any other compilers, this could just be VC fooling me again...

0

精彩评论

暂无评论...
验证码 换一张
取 消