c++ - 模板参数请引用自己的类型

标签 c++ templates c++11

这是我正在尝试做的最简单的情况:

template <template <typename...> class Wrapper>
struct WrapperTraits { };

template <typename... Whatever>
struct Foo {
private:
    // I want Foo here to refer to the template, and not the current
    // concrete type (which is injected into the namespace by default)
    using Traits = WrapperTraits<Foo>;
};

int main()
{
    return 0;
}

这是 clang 3.6 上的错误(它在 gcc 4.8 和 5.2 上编译得很好):

error: template argument for template template parameter must be a class template or type alias template
using Traits = WrapperTraits<Foo>;
^
1 error generated.
Compilation failed

这是有关 godbolt 的示例:https://goo.gl/cSx6QR

感谢您的帮助!

最佳答案

没关系,想通了。需要将其范围限定为它所在的命名空间:

template <template <typename...> class Wrapper>
struct WrapperTraits { };


template <typename... Whatever>
struct Foo {
private:
    using Traits = WrapperTraits<::Foo>; // explicit namespace
};


int main()
{
    return 0;
}

关于c++ - 模板参数请引用自己的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34075100/

相关文章:

c++ - 在 ViennaCL 中从 std::vector 转换为 ublas::compressed_matrix

c++ - Boost.Proto:如何制作原始数组的表达式终端而不是 std::vector?

c++ - 如何使用编译时常量原语类型

linux - 枚举类型声明的 C++11 编译错误如预期的那样在数字常量之前

c++ - 保存文件或文件夹的时间戳?

c++ - 用作成员构造函数参数的函数的求值顺序

c++ - 为什么我们不能使用指向派生类的指针访问 protected 基类成员?

c++ - 当考虑 "Inheritance"

ios - 在 iPad 中从基于 Window 的应用程序转换为基于 View 的应用程序

c++ - C++ 中 "unused template parameter"的最佳实践