Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc-4.9.2: non-type template parameter

Tags:

c++

gcc

templates

I have a weird compilation error on gcc-4.9.2, where the same code worked on other compilers, like gcc-4.8 or any clang I could get hold of. The problem is related to non-type template-arguments. So consider this:

#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;
}

I get the following error with 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() {}
         ^

these two ideones make it clearer:

  • gcc-4.9.2
  • gcc-4.3.2

Is this a compiler bug?

like image 644
klsdjfhsalkjfhl Avatar asked Mar 11 '15 13:03

klsdjfhsalkjfhl


1 Answers

Is this a compiler bug?

Yes, I think it was https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63658 and will be fixed in the next release.

like image 55
Jonathan Wakely Avatar answered Nov 18 '22 19:11

Jonathan Wakely