In PHP and C #, constants can be initialized as they are declared:
class Calendar3 { const int value1 = 12; const double value2 = 0.001; }
I have the following declaration of a C ++ functor, which is used with another class to compare two mathematical vectors:
struct equal_vec { bool operator() (const Vector3D& a, const Vector3D& b) const { Vector3D dist = b - a; return ( dist.length2() <= tolerance ); } static const float tolerance = 0.001; };
This code compiled without problems with g ++. Now in C ++ 0x mode (-std = C ++ 0x) the g ++ compiler displays an error message:
error: 'constexpr needed to initialize a static data class in a class, for a non-integer type
I know that I can define and initialize this static const member outside the class definition. In addition, a non-static constant data element can be initialized in the constructor initializer list.
But is there a way to initialize a constant in a class declaration in the same way as is possible in PHP or C #?
Update
I used the static only because it was possible to initialize such constants in a class declaration in g ++. I just need a way to initialize a constant in a class declaration, regardless of whether it is declared as static or not.
c ++ g ++
ezpresso Feb 04 2018-12-12T00: 00Z
source share