Understanding static constexpr member variables

前端 未结 1 1888
长情又很酷
长情又很酷 2021-01-07 18:25

I have some confusions regarding static constexpr member variables in C++11.

In first.hpp

template
struct cond_I
{ s         


        
相关标签:
1条回答
  • 2021-01-07 18:45

    According to the standard 9.4.2/p3 Static data members [class.static.data] (Emphasis Mine):

    If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression (5.20). A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [ Note: In both these cases, the member may appear in constant expressions. — end note ] The member shall still be defined in a namespace scope if it is odr-used (3.2) in the program and the namespace scope definition shall not contain an initializer.

    As M.M earlier explained in the comments ostream::operator<<(ostream&, const complex<T>&) passes by reference so value is considered odr-used in the program. Thus, as the wording above dictates you have to provide a definition.

    Now as you’ve already found out fundamental types are passed by value, that it is why no definition required.

    0 讨论(0)
提交回复
热议问题