Test: struct X { static constexpr const char x[] = "x"; }; const char X::x[]; Using g++ (GCC) 4.9.0 20130923 (experimental): g++ -c t.cc -std=c++11 t.cc:4:17: error: redeclaration 'X::x' differs in 'constexpr' const char X::x[]; ^ t.cc:2:30: error: from previous declaration 'X::x' static constexpr const char x[] = "x"; ^ t.cc:4:17: error: declaration of 'constexpr const char X::x [2]' outside of class is not definition [-fpermissive] const char X::x[]; ^ Richard Smith says: There is no rule requiring successive declarations of variables to agree in 'constexpr'ness (this rule only applies to functions). Google ref: b/10930205
Confirmed. There is no requirement that the namespace-scope definition of a static constexpr class member say its constexpr. Today's trunk (7.0) fails with the following error. All supported versions of GCC fail to compile the test case. If I'm reading the logs right, the error was introduced in r166013. $ cat xx.cpp && gcc -S -Wall -Wextra -Wpedantic -o/dev/null -std=c++11 xx.cpp struct X { static constexpr const char x[] = "x"; }; const char X::x[]; xx.cpp:5:17: error: ‘constexpr’ needed for in-class initialization of static data member ‘const char X::x [2]’ of non-integral type [-fpermissive] const char X::x[]; ^
This is fixed in 7.1.0. I'm adding a testcase and closing the bug.
Author: paolo Date: Wed Jun 14 09:18:57 2017 New Revision: 249186 URL: https://gcc.gnu.org/viewcvs?rev=249186&root=gcc&view=rev Log: 2017-06-14 Paolo Carlini <paolo.carlini@oracle.com> PR c++/58541 * g++.dg/cpp0x/constexpr-58541.C: New. Added: trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-58541.C Modified: trunk/gcc/testsuite/ChangeLog
Done.