【问题标题】:gcc-4.9.2: non-type template parametergcc-4.9.2:非类型模板参数
【发布时间】:2015-03-11 13:26:44
【问题描述】:

我在 gcc-4.9.2 上遇到了一个奇怪的编译错误,其中相同的代码在其他编译器上工作,例如 gcc-4.8 或我能掌握的任何 clang。该问题与non-type template-arguments 有关。所以考虑一下:

#include <iostream>
#include <cstddef>

int templateParam;

template <int &D> struct TestTemplate {
    int value() {}
};

template <> int TestTemplate<templateParam>::value() {
    return templateParam;
}

TestTemplate<templateParam> testVariable;

int main() {

    std::cout << testVariable.value() << "\n";

    return 0;
}

gcc-4.9.2 出现以下错误:

prog.cpp:10:17: error: prototype for 'int TestTemplate<D>::value() [with int& D = (* & templateParam)]' does not match any in class 'TestTemplate<(*  & templateParam)>'
 template <> int TestTemplate<templateParam>::value() {
                 ^
prog.cpp:7:9: error: candidate is: int TestTemplate<D>::value() [with int& D = (* & templateParam)]
     int value() {}
         ^

这两个ideones更清楚:

这是编译器错误吗?

【问题讨论】:

    标签: c++ templates gcc


    【解决方案1】:

    这是编译器错误吗?

    是的,我认为它是 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63658,并将在下一个版本中修复。

    【讨论】:

    • hmpf,不是我希望的 ;-( 无论如何谢谢。所以根据错误报告,一种解决方法是使用指针而不是引用(尝试过)
    • 补充一下,gcc-4.3.2 已经超过 6 岁了。
    • 是的,但它是 ideone 提供的...尝试了 Jessie 中可用的所有编译器...
    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多