Home > other >  What does `template<>` mean in front of a variable definition in C 98
What does `template<>` mean in front of a variable definition in C 98

Time:09-08

I'm recently working on some legacy codes written in C 98.

I am trying to get them compiled by C 17 compiler. A whole lot of warnings popped out as I was doing that. However, almost all the warnings were easily resolved. Except for this one:

struct Counter {
    Counter(int v) : m_val(v) {}
    int m_val;
};

struct AClass {
    static Counter SEQ;
};
   
template<> Counter AClass::SEQ = 0;

The C 17 compiler gave a warning "too many template headers for...(should be 0)" for the line template<> Counter AClass::SEQ = 0;. (https://godbolt.org/z/G845K3cvv)

But for C 98 compiler it was totally OK. (https://godbolt.org/z/xErje7T8o)

Now, in order to resolve the warning, I need a complete understanding as to what does the statement mean in the C 98 world.

My gut feeling is that it is a full template specialization. And the template argument is empty. So it is equivalent to Counter AClass::SEQ = 0; and template<> means nothing. (https://stackoverflow.com/a/4872816/1085251)

Can anyone tell me exactly what it means?

CodePudding user response:

It seems like gcc 4.7.4 is unable to give a diagnostic. Note that from gcc 5.1 onwards, we get a diagnostic from gcc. Demo.

The given program is ill-formed in both C 17 as well as C 98 as you're trying to provide an explicit specialization when there is nothing to specialize(as there is no templated entity anywhere in the program and AClass is also not a class template).

  • Related