【问题标题】:Define variable template in a class template在类模板中定义变量模板
【发布时间】:2016-12-20 09:08:50
【问题描述】:

考虑这个最小的例子:

template<class X> struct foo;

template<>
struct foo<int>
{
    template<class = void>
    static constexpr int x = 0;
};

template<class T>
constexpr int foo<int>::x<T>;

最后两行是必需的,否则当变量 x 被 ODR 使用时,我们将得到一个未定义的引用。

虽然 gcc (6.2.1) 很乐意编译此代码,但 clang (3.9.0) 在最后一行失败并显示以下神秘消息:

错误:变量模板部分特化不特化 任何模板参数;要定义主模板,请删除 模板参数列表

哪一个是正确的行为?

【问题讨论】:

标签: c++ gcc clang c++14


【解决方案1】:

最后两行代码应该是:

template<class T>
const int foo<int>::x;

template<class T>
constexpr int foo<int>::x;

喜欢this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多