Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why can't static const float members be initialized in a class?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
28 replies to this topic

#1 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 March 2012 - 09:56 AM

Well i was just creating my particle engine in OpenGL and i thought of using classes instead of Structs, and i eventually ran into this static const float..
I never knew they could not be initialized in Class,

For ex,

class cType
{
	 private:
	 static const int Num = 10; // Ok.
	 static const float color = ... // Error
};

Any reasons why?
Thanks!

Sponsor:

#2 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 04 March 2012 - 10:08 AM

In C++ there is no meaningful difference between class and struct.

Any reasons why?


Standard says so. There's likely obscure technical reasons for it, but there is no conspiracy of universe why it could not be done.

#3 Brother Bob   Moderators   -  Reputation: 8250

Like
1Likes
Like

Posted 04 March 2012 - 10:24 AM

If you don't want to separate the definition and the declaration, you can go with the function approach.

class cType

{

    private:

    static const int Num = 10; // Ok.

    static float color() {return ... ;}

};



#4 SiCrane   Moderators   -  Reputation: 9603

Like
1Likes
Like

Posted 04 March 2012 - 10:28 AM

Or upgrade to C++11 and use constexpr.

#5 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 March 2012 - 10:32 AM

Is it because float is old-school..
Because i can use
static const long color = ... //Works like a charm.


#6 Brother Bob   Moderators   -  Reputation: 8250

Like
0Likes
Like

Posted 04 March 2012 - 10:48 AM

As Antheus said, it's because the standard says so. It simply allows for integer types only to be initialized like that, so floating point types have to be defined the normal way with separate definition in a single translation unit. At least pre-C++11...

#7 SiCrane   Moderators   -  Reputation: 9603

Like
0Likes
Like

Posted 04 March 2012 - 10:49 AM

No, it's because float isn't a integral or enumeration type. Under C++03 those were the only kinds of static data members you could initialize in the body of a class or struct.

#8 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 March 2012 - 10:56 AM

Thanks guys!
Its funny how except double and float almost every numerical constants work..

#9 Brother Bob   Moderators   -  Reputation: 8250

Like
0Likes
Like

Posted 04 March 2012 - 11:00 AM

That's because they are integral constants.

#10 JustinDaniel   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 March 2012 - 11:06 AM

And another thing, Is long-float termed as double, because i got an error message when i used long float in class initializer

static const long float num = 10.0;
Error: a member of type "const double" cannot have an in-class initializer; //Wth?


#11 Brother Bob   Moderators   -  Reputation: 8250

Like
0Likes
Like

Posted 04 March 2012 - 11:08 AM

A float, whether it's long or not, is still not an integral type.

edit: Well, if I'm not mistaken, long isn't a valid modifier to the float type in the first place anyway. Your compiler probably accepts it as a language extension.

#12 clb   Members   -  Reputation: 1781

Like
1Likes
Like

Posted 04 March 2012 - 11:57 AM

As Antheus said, it's because the standard says so. It simply allows for integer types only to be initialized like that, so floating point types have to be defined the normal way with separate definition in a single translation unit. At least pre-C++11...

No, it's because float isn't a integral or enumeration type. Under C++03 those were the only kinds of static data members you could initialize in the body of a class or struct.


Just answering 'because they say so' is not particularly insightful. Re-asking the question as 'Why does the C++03 standard state that only integral and enum static const members may be initialized inside the class body?' is more interesting to think about. What is the technical reason, if any, for the feature being only available for ints and not floats or e.g. const char * strings, so that the wording was made that way? Given the amount of work (meetings, reviews, etc.) that is (seems to be) put into producing each version of the standard, it sounds implausible that it would have just been due to oversight. Why did they choose to restrict the feature back then?
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#13 SiCrane   Moderators   -  Reputation: 9603

Like
0Likes
Like

Posted 04 March 2012 - 12:18 PM

Nice way to take posts out of context. This is the post those were the response to:

Is it because float is old-school..
Because i can use

static const long color = ... //Works like a charm.

The long type works because it's an integral type and float doesn't because it's not a integral or enumeration type. And if you were actually paying attention to the thread rather than locking in on random details to nitpick you'd notice that the C++11 standard does currently allow for inline literal type initialization, so there currently is no technical limitation.

However, since you seem to have all the answers here, why didn't you give the reasons? Or do you just like lecturing from a position of ignorance?

#14 fastcall22   Crossbones+   -  Reputation: 4345

Like
0Likes
Like

Posted 04 March 2012 - 12:20 PM

... and i eventually ran into this static const float..
I never knew they could not be initialized in Class,


They can be initialized in the translation unit:

// cType.h

class cType {
private:
    static const int Num;
    static const float color;
};


// cType.cpp
#include "cType.h"

const int cType::Num = 10;
const float cType::color = 1.f;

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#15 L. Spiro   Crossbones+   -  Reputation: 13617

Like
3Likes
Like

Posted 04 March 2012 - 04:11 PM

Floating-point values have to be initialized at run-time because of the different FPU implementations/modes.
Integral constants are always predictable. Floating-point constants change depending on the FPU rounding method currently active when the float is modified/set. It could be rounded, truncated, etc.


If they were to be initialized in the headers where the class is defined, the actual value could differ between translation units that include that header.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#16 wqking   Members   -  Reputation: 756

Like
-1Likes
Like

Posted 04 March 2012 - 09:50 PM

Floating-point values have to be initialized at run-time because of the different FPU implementations/modes.
Integral constants are always predictable. Floating-point constants change depending on the FPU rounding method currently active when the float is modified/set. It could be rounded, truncated, etc.


That's quite good explanation.
I think it's also because of that that kind of constant is not a real constant. They have storage and may be changed.
That's why C++11 constexpr allows the initialization in header because they are real constants.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#17 SiCrane   Moderators   -  Reputation: 9603

Like
1Likes
Like

Posted 05 March 2012 - 08:13 AM

It's also an incorrect explanation. If that was the reason, then compilers like gcc wouldn't have been able to offer static const float member variables with inline definitions as extensions. And changing a variable defined as const isn't legal even if they have storage. There's nothing inherent to floating point numbers that requires them to be initialized at runtime otherwise constexpr wouldn't work with floats.

#18 L. Spiro   Crossbones+   -  Reputation: 13617

Like
0Likes
Like

Posted 05 March 2012 - 09:14 AM

It is the correct explanation. In the past compilers had no strict rules for converting floating-point values to constants. Rounding rules were not explicitly defined.

Extensions and constexpr come with explicit rules for how floating-point numbers should be converted to maintain consistency across all compilers that support those features.
IE, anything that supports the extensions is guaranteed to provide the same floating-point value, and anything that does not simply won’t compile. Hence extensions can be successful (and also why so few compilers support them).

If there was not a specifically defined method for conversion, the extension simply couldn’t be useful.


As for constexpr, it is already assumed that a lot of work will be put into a new compiler, so if the language says you have to handle floating-point conversions one way for that keyword, it is just another task to add to the stack.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#19 SiCrane   Moderators   -  Reputation: 9603

Like
0Likes
Like

Posted 05 March 2012 - 04:17 PM

It is the correct explanation.

Source?

#20 wqking   Members   -  Reputation: 756

Like
-1Likes
Like

Posted 05 March 2012 - 08:38 PM


It is the correct explanation.

Source?

Not reliable source, but
http://stackoverflow.com/questions/1907214/why-are-inlined-static-consts-not-allowed-except-ints
has some discussion on it.
One main opinion is that float number may have different value between the compiling machine and execute machine.

For constexpr, I believe the standard has clearly definition and restriction on it to eliminate the difference.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS