c++ - Why does GCC think that the definition of a constexpr static data member must be marked constexpr? -


[c++14: 7.1.5/1]: constexpr specifier shall applied definition of variable or variable template, declaration of function or function template, or declaration of static data member of literal type (3.9). if declaration of function, function template, or variable template has constexpr specifier, declarations shall contain constexpr specifier. [..]

notice second sentence not mention "a static data member" way first sentence does, there no requirement in passage declarations (and here i'm considering defining declaration specifically) of constexpr static data member have constexpr specifier.

i can't find rule elsewhere mandate this, either.

why, then, gcc reject following program?

#include <chrono>  using namespace std::chrono_literals;  #define dur 1000ms  struct t {    static constexpr auto dur_1 = dur; };  decltype(t::dur_1) t::dur_1;  // main.cpp:12:23: error: 'constexpr' needed in-class initialization of static data member 'const std::chrono::duration<long int, std::ratio<1l, 1000l> t::dur_1' of non-integral type [-fpermissive]  // decltype(t::dur_1) t::dur_1; //                       ^ 

this looks underspecified me, don't see explicit requirement can see why issue defect report 699: must constexpr member functions defined in class member-specification? although dealing constexpr member functions says following (emphasis mine):

if prohibition relaxed allow separate declaration , definition of constexpr member functions, questions need answered, such whether constexpr specifier must appear on both declaration , definition (the inline specifier need not). if can omitted in 1 or other, there's usability issue regarding fact constexpr implies const; const qualifier need specified explicitly in declaration in constexpr omitted.

although in case adding const not solve problem although in simpler cases seem solve issue. can see in simpler case both clang , gcc require either const or constexpr:

struct t {    static constexpr int blah = 1 ; };  const int t::blah ; 

update

this gcc bug report: bogus "error: redeclaration ... differs in ‘constexpr’" has following quote richard smith:

there no rule requiring successive declarations of variables agree in 'constexpr'ness (this rule applies functions).

so looks gcc bug, although still seems use clarity in standard.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -