我在gcc-4.9.2上有一个奇怪的编译错误,其中相同的代码在其他编译器上运行,例如gcc-4.8或任何我可以抓住的clang.该问题与非类型模板参数有关.所以考虑一下:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我用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() {}
^
Run Code Online (Sandbox Code Playgroud)
这两个想法让它更清晰:
这是编译器错误吗?