是否可以创建模板别名
Is it possible to create a template alias?
请考虑以下代码:
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 > >();
}
现场示例
问:有没有办法创建某种类似于类型别名的"模板别名"?(请参阅上例中的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...>;
因此,使用"本机"模板Z
,您将执行Z<Ts...>
。 使用这些"间接"模板,您可以apply_t<Z, Ts...>
.
使用此约定,using
别名的模板可以返回间接模板。 如果代码的其余部分遵循始终调用apply_t
来应用模板的约定,并且您间接化您编写的所有其他模板,则我们就完成了。
这很丑陋。
相关文章:
- 部分定义/别名模板模板参数
- 如何在C++20中创建模板别名的推导指南
- 告诉c++编译器该参数没有别名
- boost::spirit::karma 替代生成器,带有 boost::variant 由字符串和字符串别名组成
- 继承模板类中的类型别名
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 为什么 GCC 在使用类型别名时处理 const reinterpret_cast不同?
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 如何使用类型别名从模板化类中隐藏模板列表
- 满意和建模的概念?
- 模板模板参数和模板别名:编译器错误?
- 使用定义函数模板别名
- 为模板参数包添加别名
- 如何检测类型类型别名?
- C++类型别名,其中值被替换
- C++使用默认模板参数键入别名和转发声明
- 在Qt中注册自定义元类型的别名类型
- 缺少别名模板C++参数列表