https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92576

            Bug ID: 92576
           Summary: Definition of variable template without initializer is
                    treated as declaration
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

GCC accepts this as a declaration followed by a definition, but it should be
two definitions (with the first being ill-formed):

template<typename T> constexpr bool var;
template<typename T> constexpr bool var = false;


Clang says:

inline.cc:1:37: error: default initialization of an object of const type 'const
bool'
template<typename T> constexpr bool var;
                                    ^
                                        = false
inline.cc:2:37: error: redefinition of 'var'
template<typename T> constexpr bool var = false;
                                    ^
inline.cc:1:37: note: previous definition is here
template<typename T> constexpr bool var;
                                    ^
2 errors generated.

EDG says:

"inline.cc", line 1: error: missing initializer for constexpr variable
  template<typename T> constexpr bool var;
                       ^

"inline.cc", line 2: error: variable template "var" has already been defined
  template<typename T> constexpr bool var = false;
                                      ^

"inline.cc", line 2: error: "constexpr" is not valid here
  template<typename T> constexpr bool var = false;
                       ^

3 errors detected in the compilation of "inline.cc".


MSVC says:

example.cpp
<source>(1): error C2737: 'var': 'constexpr' object must be initialized
<source>(2): error C2086: 'const bool var': redefinition
<source>(1): note: see declaration of 'var'
Compiler returned: 2


I think 'extern' is needed to stop the first line being a definition:

template<typename T> extern constexpr bool var;
template<typename T> constexpr bool var = false;


G++ correctly rejects it for a non-template inline variable:

constexpr bool var;
constexpr bool var = false;

inline.cc:1:16: error: uninitialized const ‘var’ [-fpermissive]
 constexpr bool var;
                ^~~
inline.cc:2:16: error: redefinition of ‘constexpr const bool var’
 constexpr bool var = false;
                ^~~
inline.cc:1:16: note: ‘constexpr const bool var’ previously declared here
 constexpr bool var;
                ^~~

Reply via email to