7

The following code (taken from Wikipedia) defines the variable template pi<>:

template<typename T=double>
constexpr T pi = T(3.14159265358979323846264338328);
template<>
constexpr const char* pi<const char*> = "π";

With the clang compiler (Apple clang version 12.0.0) (with C++14), this triggers a warning (with -Weverything):

no previous extern declaration for non-static variable 'pi<const char *>'

declare 'static' if the variable is not intended to be used outside of this translation unit

Moreover, since this was defined in a header, multiple instances of 'myNameSpace::pi<char const*>' were created, causing linker errors.

So, as suggested, I added the static keyword, which silenced the warning:

template<>
static constexpr const char* pi<const char*> = "π";

But now gcc (9.3.0) is unhappy, giving an error pointing at the static keyword:

error: explicit template specialization cannot have a storage class

What is the correct way to avoid either warning and error?

6
  • Can't reproduce the warning on the Clang 11 (non-apple). Feb 18, 2021 at 18:43
  • What compiler settings are you using? I can't reproduce the warning here Feb 18, 2021 at 18:44
  • It's clangs -Wmissing-variable-declarations I suspect gcc is correct and would silence that warning from clang using -Wno-missing-variable-declarations
    – Ted Lyngmo
    Feb 18, 2021 at 18:46
  • 2
    @TedLyngmo Thanks for that. Now I can confirm. OP, are you stuck with C++14? In C++17 you can use template<typename T=double> constexpr inline T pi = T(3.14159265358979323846264338328); template<> constexpr inline const char* pi<const char*> = "π"; which removes the warning. Feb 18, 2021 at 18:49
  • @molbdnilo yes, -Weverything. See edited post, also note the linker error
    – Walter
    Feb 18, 2021 at 18:52

1 Answer 1

4

The warning from (this old version of) Clang is partly misleading, but does indicate the real problem that you eventually encountered with the linker. The warning describes the good rule of thumb that a global variable ought to

  1. appear with extern in a header and then without in a source file, or
  2. appear with static in a source file (avoiding collisions with any other symbol).

The latter choice doesn't apply to explicit specializations: since linkage applies to templates as a whole (the standard says that it pertains to the name of the template, which is evocative even if it doesn't work well for overloaded functions), you can't make just one specialization static and Clang is incorrect to accept it. (MSVC also incorrectly accepts this.) The only way to make a "file-local specialization" is to use a template argument that is a local type, template, or object. You can of course make the whole variable template have internal linkage with static or an unnamed namespace.

However, the former choice does apply: an explicit specialization is not a template, so it must be defined exactly once (in a source file). Like any other global variable, you use extern to reduce the definition to a declaration:

// pi.hh (excerpt)
template<typename T=double>
constexpr T pi = T(3.14159265358979323846264338328);
template<>
extern constexpr const char* pi<const char*>;

// pi.cc
#include"pi.hh"
template<>
constexpr const char* pi<const char*> = "π";

(Since the primary template is, well, a template, it is defined in the header file.)

As mentioned in the comments, C++17 allows inline variables; your explicit specialization again behaves like an ordinary global variable and can be defined with inline in a header if desired.

3
  • Thanks, good answer!. However it implies that the example on Wikipedia is wrong or at least misleading
    – Walter
    Feb 20, 2021 at 20:19
  • @Walter: You’re welcome. Aside from concerns of brevity and the same rules applying to (non-inline) function template specializations, the example isn’t wrong for a source file—and who says it’s not an excerpt from within an unnamed namespace? Feb 20, 2021 at 21:26
  • No, it's not wrong, but certainly misleading. I expect better from Wikipedia.
    – Walter
    Feb 22, 2021 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.