gpt4 book ai didi

c++ - gcc-4.9.2 : non-type template parameter

转载 作者:可可西里 更新时间:2023-11-01 17:57:39 27 4
gpt4 key购买 nike

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

这两个概念让它更清晰:

这是编译器错误吗?

最佳答案

Is this a compiler bug?

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

关于c++ - gcc-4.9.2 : non-type template parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28987859/

27 4 0