I have some constants that need to be used only at compile time to simplify the code, so I don't need the actual variables available at runtime.
Traditionally, this was done with #define NAME 123 , but I would like to use type safe.
Outside of classes, you can const int name = 123; which works fine, but it doesn't seem possible to put it inside a class. For instance:
class Example { public: const double usPerSec = 1000000.0; }; double usOneMinute = 60 * Tempo::usPerSec;
Works with Visual C ++, but does not work with GCC:
error: non-static const member 'const double Example::usPerSec', can't use default assignment operator
You can fix this by making it static, but then Visual C ++ complains:
error C2864: 'Example::usPerSec' : a static data member with an in-class initializer must have non-volatile const integral type type is 'const double'
I assume that VC ++ will only accept static const int .
I want to avoid setting the value in the constructor, because then I need an instance of the class at runtime to access the value, while in fact I want all this to be processed at compile time, for example with #define .
So, how can I define a constant as double inside a class without resorting to its global use or using #define , which will work without an instance of the class and work with the main C + +03 compilers?