constant variables not working in header

后端 未结 10 2008
悲&欢浪女
悲&欢浪女 2020-11-30 20:10

if I define my constant varibles in my header like this...

extern const double PI = 3.1415926535;
extern const double PI_under_180 = 180.0f / PI;
extern cons         


        
10条回答
  •  猫巷女王i
    2020-11-30 20:21

    An old question, indeed, but one useful answer is missing.

    It is possible to trick MSVC into accepting static constants in headers simply by wrapping those in a "dummy" class template:

    template 
    struct C {
         static const double Pi;
    };
    
    template 
    const double C::Pi = 3.14159;
    

    Now, C<>::PI can be accessed from elsewhere. No redefinition complains; constant is directly accessible in each compilation units without fancy link time optimization. Macro can be rolled out to further prettify this approach (even though macros are evil).

提交回复
热议问题