Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

c++ - Mixing constexpr declarations and const definitions

I came across the following situation:

struct Foo
{
    static constexpr char s[] = "Hello world";
};

const char Foo::s[];

This code snippet compiles with Clang 3.7 (with -std=c++11 and -std=c++14), but GCC (4.8, 6.0, same language settings) gives the error I would have expected:

GCC 4.8:

in.cpp:6:19: error: redeclaration ‘Foo::s’ differs in ‘constexpr’
 const char Foo::s[];
                   ^
in.cpp:3:27: error: from previous declaration ‘Foo::s’
     static constexpr char s[] = "Hello world";
                           ^
in.cpp:6:19: error: declaration of ‘constexpr const char Foo::s [12]’ outside of class is not definition [-fpermissive]
 const char Foo::s[];

GCC 6.0:

‘constexpr’ needed for in-class initialization of static data member ‘const char Foo::s [12]’ of non-integral type [-fpermissive]

I found this old question that seems to discuss mixing constexpr and const, but it focusses on whether initializers are constant expressions, rather on whether definition and declaration can differ with regard to constness.

Is it allowed to provide the definition for a constexpr T static data member as a const T?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code is well-formed. The constexpr-specifier is not itself part of the type but adds const ([dcl.constexpr]/9), which is present in your second declaration. Although different declarations of one function (or function template) have to agree in constexpr-ness as per [dcl.constexpr]/1, no such rule exists for variable declarations.

See bug #58541, which basically uses your example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...