1

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?

4
  • I don't think it's a Standard change. Might be a defect fix or or a bug in older compilers. Or since it's a warning and not a hard error, it might be something that's now reported and previously wasn't. Commented Sep 8, 2022 at 7:02
  • You get a warning with gcc 5.1 onwards. See demo. Also the program is ill-formed. Commented Sep 8, 2022 at 7:03
  • 1
    It's a template specialisation. I'd argue that the C++98 compiler was incorrect in not diagnosing an error since none of the types/objects in your sample are templates that need to be specialised. (I'd also guess the template<> is either a typo, or an artefact resulting from editing a templated type to produced non-templated type). You may wish to look at stackoverflow.com/questions/4872809/… Commented Sep 8, 2022 at 7:04
  • 1
    This is the emulation of inline static Counter SEQ = 0; in the class definition. Commented Sep 8, 2022 at 7:04

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.