Dan*_*rey 13 c++ c++11 type-alias c++14
请考虑以下代码:
template< template< typename ... > class ... Ts >
struct unite
{
template< typename ... T >
struct type
: Ts< T ... > ...
{ };
};
// This does not work as ::type does not name a type, but a template:
// template< template< typename ... > class ... Ts >
// using unite_t = typename unite< Ts ... >::type;
template< typename > struct debug_none {};
template< typename > struct debug_cout {};
template< typename ... > struct raise_demangled {};
template< typename ... > struct raise_specialized {};
template< typename, typename = int > struct match_default {};
template< template< typename ... > class Control >
void f()
{}
int main()
{
f< unite< debug_none, raise_demangled, match_default >::type >();
// Is there any way to create something like unite_t which works like this:
// f< unite_t< debug_none, raise_demangled, match_default > >();
}
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法创建某种类型别名的"模板别名"?(见上unite_t例)
你不能.
using可以"返回"一个类型或一个变量.它不能"回归" template.其他地方没有类似的机制.
你可以通过采用所有模板都不是模板的约定来做一些模糊的事情,而是template<?>using apply=?;在其中包含别名的类(当我们在它的时候,常量是std::integral_constants<T,?>,并且指针是pointer_constant<T*,?>).
现在一切都是一个班级. template成为一种类型(有一个::apply<?...>.
将一组类型应用于此类模板将通过以下方式完成:
template<class Z, class...Ts>
using apply_t = Z::template apply<Ts...>;
Run Code Online (Sandbox Code Playgroud)
所以使用"原生"模板Z,你会做到Z<Ts...>.有了这些"间接"模板,你就可以了apply_t<Z, Ts...>.
使用此约定,模板using别名可以返回间接模板.如果您的其余代码遵循始终要求apply_t应用模板的约定,并且您间接地使用您编写的所有其他模板,那么我们就完成了.
这很难看.