Ang*_*iel 34 c++ templates using c++11 c++14
我的目标是在使用模板模板参数定义其他类型时记录模板化类型的名称.我遇到的问题是结果类型被认为是不等价的.
为了实现这一点,我尝试使用模板化的"使用"别名来记录作为模板模板参数给出的名称.然后我引用别名来恢复名称.这有用的一个例子是当我有一个模板化函数,它返回一个带有需要推导出的模板模板参数的类型.
以下代码显示了我的方法:
include <type_traits>
template<typename X>
struct TT {};
template<template<typename> class T>
struct UU {
template<typename X>
using name = T<X>;
};
template<template<typename> class T>
struct VV {};
int main() {
static_assert(std::is_same<
TT<int>,
UU<TT>::template name<int>
>::value, "Templated type instances are distinct");
static_assert(std::is_same<
VV<TT>,
VV<UU<TT>::template name>
>::value, "Template type instances using template names are distinct");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译如下:
c++ -std=c++14 Test.cpp -o Test
Run Code Online (Sandbox Code Playgroud)
产量:
Test.cpp:20:3: error: static_assert failed "Template type instances using template names are distinct"
Run Code Online (Sandbox Code Playgroud)
还有另一种录制和使用模板名称的方法吗?
有人可以指示我指定此行为的标准部分吗?
| 归档时间: |
|
| 查看次数: |
1009 次 |
| 最近记录: |