Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't static const floats allowed?

Tags:

c++

visual-c++

I have a class which is essentially just holds a bunch of constant definitions used through my application. For some reason though, longs compile but floats do not:

class MY_CONSTS { public :     static const long   LONG_CONST = 1;      // Compiles      static const float FLOAT_CONST = 0.001f; // C2864 }; 

Gives the following error:

1>c:\projects\myproject\Constant_definitions.h(71) : error C2864: 'MY_CONSTS::FLOAT_CONST' : only static const integral data members can be initialized within a class 

Am I missing something?

like image 851
Jon Cage Avatar asked Mar 16 '10 11:03

Jon Cage


People also ask

Can const and static used together?

So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.

What is const float in C++?

Floating-point constants specify values that must have a fractional part. Floating-point constants have a "mantissa," which specifies the value of the number, an "exponent," which specifies the magnitude of the number, and an optional suffix that specifies the constant's type(double or float).

Can static members be const?

A static data member can be of any type except for void or void qualified with const or volatile. You cannot declare a static data member as mutable.

Can we use static const in C?

const means that you're not changing the value after it has been initialised. static inside a function means the variable will exist before and after the function has executed. static outside of a function means that the scope of the symbol marked static is limited to that . c file and cannot be seen outside of it.


1 Answers

To answer the actual question you asked: "because the standard says so".

Only variables of static, constant, integral types (including enumerations) may be initialized inside of a class declaration. If a compiler supports in-line initialization of floats, it is an extension. As others pointed out, the way to deal with static, constant, non-integral variables is to define and initialize them in the class's corresponding source file (not the header).

C++ Standard Section 9.2 "Class Members" item 4:

A member-declarator can contain a constant-initializer only if it declares a static member (9.4) of const integral or const enumeration type, see 9.4.2.

Section 9.4.2 "Static Data Members" item 2:

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.

like image 171
Tyler McHenry Avatar answered Sep 20 '22 01:09

Tyler McHenry