Constexpr static function template: g ++ error is a warning about clang

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 well with both g ++ and clang. Very well. 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 specification cannot have storage class

and clang 3.8 too:

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

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

g ++ 6.1:

11: error: explicit template specification cannot have storage class

clang 3.8 compilation with warning:

11: warning: explicit specialization may not have a storage class

and the result of clang returns the correct answer.

Is this a bug in clang? If not, then in this case it makes sense not to throw an error?

+7
c ++ language-lawyer templates function-templates
source share
1 answer

It is as simple as [dcl.stc] / 1 (which is already mentioned in C ++ 98):

A storage class thread_local other than thread_local should not be specified in explicit specialization (14.7.3) or in an explicit instance directive (14.7.2).

+2
source share

All Articles