Why can't static const float class members be inititalized in the class?

M

Martin Rennix

Eg

class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};

What's the reasoning behind this seemingly arbitrary limitation?

Martin
 
V

Victor Bazarov

Martin said:
class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};

What's the reasoning behind this seemingly arbitrary limitation?

I would guess portability, but don't quote me on that.

Ask in comp.std.c++ -- they hold/discuss the reasoning behind any
decisions that made it to the Standard.

V
 
R

Ron Natalie

Martin said:
Eg

class Foo
{
static const float f = 0.0f; // error
static const int i = 0; // ok
};

What's the reasoning behind this seemingly arbitrary limitation?

Martin
The compiler is only required to know how to do integer math at compile
time.
 
M

Martin Rennix

MartinRennixwrote:




The compiler is only required to know how to do integer math at compile
time.

Is that because in the "olden days" you couldn't guarantee that a FPU
would be available, or the equivalent software floating point library?
 
J

James Kanze

The compiler is only required to know how to do integer math at compile
time.
[/QUOTE]

Also, the fact that there is no need for it. It's really a
hack, which was added for integers is because certain uses
require an integral constant expression.
Is that because in the "olden days" you couldn't guarantee that a FPU
would be available, or the equivalent software floating point library?

It's because even today, cross-compilers exist, and floating
point results depend on the actual floating point being used.
Something like 2. * 3.14 will have different values on different
machines. And the standard doesn't impose emulation of the
target floating point hardware.
 
R

Ron Natalie

Martin said:
Is that because in the "olden days" you couldn't guarantee that a FPU
would be available, or the equivalent software floating point library?
I think it was primarily there for crosscompilers. Integer math is
pretty easy to do universally, floating point is a bit more involved.
Not everybody uses the IEEE fp math.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top