【问题标题】:Refer to own type for a template template parameter [duplicate]请参阅自己的类型以获取模板模板参数[重复]
【发布时间】:2015-12-03 20:01:35
【问题描述】:

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

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&lt;class...Ts&gt;using Foo_Z=Foo&lt;Ts...&gt;; 然后使用Foo_Z?
  • 无法重现错误:ideone.com/rXiUhl
  • @tobi303 这是在 gcc 5.1 上。它无法在 clang 上编译。我在godbolt上提供了一个链接
  • 对不起,我错过了“工作正常...”这一点
  • 已知的 clang 错误。另一种解决方法是写Foo::template Foo

标签: c++ templates c++11


【解决方案1】:

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

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


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


int main()
{
    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多