模板别名 [英] template aliases
问题描述
In VS 2013 under Windows & Pro SP1 I wrote the code for TypeList:
#include< tchar.h>
#include< type_traits>使用namespace std
;
class NullType;
模板< class ... Ts>
struct mp_typelist {};
////////////////////////////////////////// /////////////////////////////////////
//搜索类型
模板< int k,class T,class ... Ts>
struct mp_index_of;
template< int k,class T>
struct mp_index_of< k,T,mp_typelist< NullType> >
{
使用mp_type = typename std :: integral_constant< int,-1> ;;
};
/ *
模板< int k,class T,class H>
struct mp_index_of< k,T,mp_typelist< H> >
{
enum {val = = is_same< T,H> :: value? k + 1:-1};
使用mp_type = integral_constant< int,val> ;;
};
* /
模板< int k,class T,class ... Ts>
struct mp_index_of< k,T,mp_typelist< T,Ts ...>>
{
使用mp_type = typename std :: integral_constant< int,k> ;;
};
template< int k,class T,class H,class ... Ts>
struct mp_index_of< k,T,mp_typelist< H,Ts ...> >
{
使用mp_type = typename mp_index_of< k + 1,T,mp_typelist< Ts ...> > :: mp_type;
};
////////////////////////////////////////// ////////////////////////////////////////
int _tmain (int argc,_TCHAR * argv [])
{
using type_list = mp_typelist< char,int,long,float,double,void,NullType> ;使用mp_ret_type = mp_index_of<
0,double,type_list> :: mp_type;
int res = mp_ret_type :: value;
// int res2 = = typrname mp_index_of< 0,double,type_list> :: mp_type ::值;
bool bComp = is_same< mp_ret_type,integral_constant< int,0>> :: value;
返回0;
}
#include <tchar.h> #include <type_traits> using namespace std; class NullType; template <class... Ts> struct mp_typelist {}; /////////////////////////////////////////////////////////////////////////////// // Search for a type template <int k, class T, class... Ts> struct mp_index_of; template <int k, class T> struct mp_index_of <k, T, mp_typelist<NullType> > { using mp_type = typename std::integral_constant < int, -1 >; }; /* template <int k, class T, class H> struct mp_index_of < k, T, mp_typelist<H> > { enum { val = = is_same<T, H>::value ? k + 1 : -1 }; using mp_type = integral_constant<int, val>; }; */ template <int k, class T, class... Ts> struct mp_index_of <k, T, mp_typelist < T, Ts... >> { using mp_type = typename std::integral_constant < int, k >; }; template <int k, class T, class H, class... Ts> struct mp_index_of < k, T, mp_typelist<H, Ts...> > { using mp_type = typename mp_index_of < k + 1, T, mp_typelist<Ts...> >::mp_type; }; ////////////////////////////////////////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { using type_list = mp_typelist < char, int, long, float, double, void, NullType > ; using mp_ret_type = mp_index_of < 0, double, type_list> ::mp_type; int res = mp_ret_type::value; //int res2 = = typrname mp_index_of < 0, double, type_list> ::mp_type::value; bool bComp = is_same<mp_ret_type, integral_constant<int, 0>>::value; return 0; }
代码编译并运行正常。
但如果我取消注释该行:
But if I uncomment the line:
int res2 = = mp_index_of < 0, double, type_list> ::mp_type::value;
我收到错误消息:
'Intellisense需要一个表达式'和
'Intellisense expects an expression' and
'错误C2059语法错误:'='。'
'Error C2059 syntax error:'='.'
任何解释?
另外我不知道如何自动在第一次调用mp_index_of时提供k = 0,以及如何去除NullType。注释掉mp_index_of的专业化不起作用。
Also I do not know how to automatically supply k = 0 in the first call to mp_index_of, and how to get rid of NullType. Commented out specialization of mp_index_of does not work.
推荐答案
但如果我取消注释该行:
But if I uncomment the line:
int res2 = = mp_index_of < 0, double, type_list> ::mp_type::value;
我收到错误消息:
'Intellisense需要一个表达式'和
'Intellisense expects an expression' and
'错误C2059语法错误:'='。'
'Error C2059 syntax error:'='.'
连续两个等号。删除一个。
You have two equal signs in a row. Remove one.
这篇关于模板别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!