【问题标题】:Warning about too many template headers关于模板标题过多的警告
【发布时间】:2018-04-27 09:29:18
【问题描述】:

我收到与previous discussion 相同的警告消息,但我不明白如何解决:

警告:foo::value 的模板头太多(应该为 0) int const foo::value = 1;

当你想使用以下玩具头时会出现警告消息:

#ifndef FOO
#define FOO

template<int T>
struct foo;

template<>
struct foo<1>
{ static int const value; };

template<>
int const foo<1>::value = 1;

#endif

你能解释一下这是什么问题吗?

【问题讨论】:

    标签: c++ c++14


    【解决方案1】:

    您对foo&lt;1&gt;::value 的定义中的template&lt;&gt; 是无关的。删除它:

    template<int T>
    struct foo;
    
    template<>
    struct foo<1>
    { static int const value; };
    
    int const foo<1>::value = 1;
    

    clang++ 给你一个更好的错误:

    prog.cc:11:1: error: extraneous 'template<>' in declaration of variable 'value'
    template<>
    ^~~~~~~~~~
    1 error generated.
    

    live example on wandbox

    【讨论】:

    • 非常感谢维托里奥。你的答案很完美。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    相关资源
    最近更新 更多