C++ 相当于为“模板”使用“别名”
C++11添加了别名模板,例如:C++ 相当于为“模板”使用“别名”,c++,templates,c++11,c++14,using,C++,Templates,C++11,C++14,Using,C++11添加了别名模板,例如: template<typename T> using identity = T; template<bool b, typename T = void> using EnableIf = typename std::enable_if<b, T>::type; 我们可以这样使用: template<typename T> struct my_meta { template<typename U>
template<typename T> using identity = T;
template<bool b, typename T = void> using EnableIf = typename std::enable_if<b, T>::type;
我们可以这样使用:
template<typename T>
struct my_meta {
template<typename U>
using Template = identity<U>;
};
template<typename T>
struct my_meta {
template<typename U>
using Template = int;
};
template<typename T, typename U>
typename my_meta<T>::template Template<U>
do_stuff( U&& ) { return {}; }
甚至
template<template<typename> class Template>
void do_more_stuff() {}
template<typename T>
void do_stuff() {
// syntax I want: just produce an alias or class template directly:
do_more_stuff< my_meta_template<T> >();
// vs what I find is required: the disambiguator:
do_more_stuff< my_meta<T>::template Template >();
};
模板
void do_more_stuff(){}
模板
void do_stuff(){
//语法:直接生成别名或类模板:
做更多的东西();
//vs我发现的是必需的:消歧器:
做更多的东西();
};
我所知道的移除模板的最佳方法是制作简单的翘曲器,它可以为您完成以下操作:
template<typename Meta, typename U>
using apply = typename Meta::template Template<U>;
这对嵌套类型很好,但对其他上下文(如调用依赖类型的成员函数)没有帮助:t.template f()
如果f
是固定的,可以用类似的方式解决:template auto call\u f(t\u This,Args&&…a)->decltype(\u This.template f(std::forward(a))){return\u This.template f(std::forward(a)…);}
template<typename Meta, typename U>
using apply = typename Meta::template Template<U>;
template<typename Meta>
void do_more_stuff()
{
apply<Meta, int>::func(); //=== Meta::template Template<int>::func();
}
template<typename T>
void do_stuff() {
do_more_stuff< my_meta<T> >();
do_more_stuff< my_meta<T> >();
};