I know I can define and initialize this static const member outside of the class definition. Also, a non-static constant data member can be initialized in the initializer list of a constructor.

But is there any way to initialize a constant within class declaration just like it is possible in PHP or C#?

Update

I used static keyword just because it was possible to initialize such constants within the class declaration in g++. I just need a way to initialize a constant in a class declaration no matter if it declared as static or not.

I used static keyword just because it was possible to initialize such constants within the class declaration in g++. I just need a way to initialize a constant in a class declaration no matter if it declared as static or not. That's the wrong way to decide whether a member should be static or not. Never let lexical laziness decide the semantics of your code.
–
Lightness Races in OrbitFeb 11 '12 at 18:17

That's the wrong way to decide whether a member should be static or not. I don't agree. I think that does not matter for constant members.
–
ezpressoFeb 11 '12 at 19:05

3

@expresso: Not at all. You can initialise a non-static constant member with instance-specific information. That you've decided that your constant is a property of the type rather than of a specific instance is the reason to make it static, not because you fancied a typing shortcut.
–
Lightness Races in OrbitFeb 11 '12 at 19:31

@lightless: Well, it is possible, but I don't see any reason for making use of initialization of same instance-specific constants with different values. I used to use non-const class fields for that!
–
ezpressoFeb 11 '12 at 20:06

3

Why, if they never change after object instantiation? struct myType { const std::time_t instantiated; myType() : instantiated(std::time(0)) {} }; Everything that can be constshould be const; that applies to static and non-static members alike.
–
Lightness Races in OrbitFeb 11 '12 at 20:40

In this case, the i member of all instances of class X is initialized to 5 by the compiler-generated constructor, and the f member is initialized to 3.12. The static const data member j is initialized to 42, and the static constexpr data member g is initialized to 9.5.

Since float and double are not of integral or enumeration type, such members must either be constexpr, or non-static in order for the initializer in the class definition to be permitted.

Prior to C++11, only static const data members of integral or enumeration type could have initializers in the class definition.

Initializing static member variables other than const int types is not standard C++ prior C++11. The gcc compiler will not warn you about this (and produce useful code nonetheless) unless you specify the -pedantic option. You then should get an error similiar to:

If this struct isn't in a header (I assume it's a convenience functor), you can use an anonymous namespace and a const float instead. #define should only be used when necessary.
–
Peter WoodFeb 15 '12 at 21:01

3

Using a macro is valid, but it's generally considered not to be best practice in C++ code.
–
monkey_05_06Jan 12 '13 at 0:25