您的位置:首页 > 编程语言 > C语言/C++

How to get a type in C++ when its template argument is the argument

2011-08-21 17:30 726 查看
First, we can't do :

//It's wrong.

template <typename U, typename T>
U TypeFunc (U<T> p);
template <typename U, typename T>U<T> TypeFunc2();//either is wrong__typeof (TypeFunc(b))<int> p1;__typeof (TypeFunc2(b)) p2


Under some certain compilers' C++ dialect:

//It's ok

template < template <typename T> class U, typename V>
U<V> typefunc( U<T> tmpl, V type);

vector<int>b;
char c;
__typeof (typefunc(b,c)) u;
//u's type is vector<char>
In C++0x

//It's simple
template <typename T>
using V<T> = std::vector<T>;

V<int> a;




How ever, in GCC without 0x support we can do this by:
template <typename T, template <typename T> class U, typename T2>

U<T2> typefunc(U<T> a, T2 b);

vector <int> a;
char b;
__typeof (typefunc(a, b)) c;
//c's type is vector<char> by adding extra function template argument T
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐