使用别名模板名称

时间:2016-02-19 13:27:13

标签: c++ templates c++11 c++14 using

我的目标是在使用模板模板参数定义其他类型时记录模板化类型的名称。我遇到的问题是结果类型被认为是不等价的。

为了达到这个目的,我尝试使用模板化的"使用"别名,用于记录作为模板模板参数给出的名称。然后我引用别名来恢复名称。这个有用的例子是当我有一个模板化函数,它返回一个带有需要推导出的模板模板参数的类型。

以下代码显示了我的方法:

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;
}

编译如下:

c++ -std=c++14 Test.cpp -o Test

收率:

Test.cpp:20:3: error: static_assert failed "Template type instances using template names are distinct"

是否有另一种录制和使用模板名称的方式?

有人可以指示我指定此行为的标准部分吗?

0 个答案:

没有答案