Is it legal C++ to pass the address of a static const int with no definition to a template?

后端 未结 4 421
刺人心
刺人心 2021-01-15 11:53

I\'m having trouble deciding whether not this code should compile or if just both compilers I tried have a bug (GCC 4.2 and Sun Studio 12). In general, if you have a static

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-15 12:15

    To be a well formed program you stil have to have the defintion of the static variable (without an initializer in this case) if it actually gets used, and taking the address counts as a use:

    • C++2003 Standard: 9.4.2 Static data members Paragraph 4 (bold added)

    If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression (5.19). In that case, the member can appear in integral constant expressions. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer

提交回复
热议问题