gcc-4.9.2: 非类型模板参数

gcc-4.9.2: non-type template parameter

我在 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() {}
         ^

这两个ideone更清楚:

这是编译器错误吗?

Is this a compiler bug?

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