constexpr static template function: g++ error is a warning on clang
Asked Answered
W

1

8

Consider the following snippet:

#include <iostream>

template <int I>
constexpr int f() { return I * f<I-1>(); }

template<>
constexpr int f<0>() { return 1; }


int main () {
  std::cout << f<5>();
  return 0;
}

This code compiles nicely with both g++ and clang. Very nice. Now add static to the template function specialization:

template<>
constexpr static int f<0>() { return 1; }

then g++ 6.1 reacts with an error:

11 : error: explicit template specialization cannot have a storage class

and clang 3.8 too:

11 : error: explicit specialization has extraneous, inconsistent storage class 'static'

They look like in agreement. Very nice again. Now, add static keyword also the template function general case:

g++ 6.1:

11 : error: explicit template specialization cannot have a storage class

clang 3.8 compiles with a warning:

11 : warning: explicit specialization cannot have a storage class

and clang result returns the correct answer.

Is this a bug in clang? If not, in which case does it make sense not to throw an error?

Widow answered 17/6, 2016 at 10:46 Comment(3)
I say, this is an error. g++ is correct.Dementia
What do you mean is this a bug? You were issued a diagnostic so there is something wrong with the code. Most of the time you should use -Werror which will halt compilation.Carboloy
@NathanOliver: what does the standard says ? Have you gone thorugh the standard ?Concenter
A
4

It's as simple as [dcl.stc]/1 (which goes as far back as C++98):

A storage-class-specifier other than thread_local shall not be specified in an explicit specialization (14.7.3) or an explicit instantiation (14.7.2) directive.

Ambros answered 17/6, 2016 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.