About static const members appearing in another static const definitions

R

Rakesh Sinha

I am having this code here.


static const float PI = 3.14159;
static const float INC = 0.4f * PI;

When I compile my program, I get the following error,

error: `MyClass::pI' cannot appear in a constant-expression .

I was wondering why this is not possible here. May be is it because
there is no guaranteed order of initialization among C++ members ?

But when, intuitively, I had defined PI here, and then use PI later in
a subsequent definition. Would it not be possible to use PI
here,(since i can't change PI anymore ) ?
 
V

Victor Bazarov

Rakesh Sinha said:
I am having this code here.
Where?

static const float PI = 3.14159;
static const float INC = 0.4f * PI;

Only static data members of integral types can be initialised inside
the class definition. Please in the future don't make us guess or
read your mind. As much as we can do it successfully, it still takes
a lot of energy, which we have a very limited amount to spare.
When I compile my program, I get the following error,

error: `MyClass::pI' cannot appear in a constant-expression .

I was wondering why this is not possible here. May be is it because
there is no guaranteed order of initialization among C++ members ?

No, the order of initialisation is well-defined. It's simply not
allowed to initialise a floating point member in the class definition.
But when, intuitively, I had defined PI here, and then use PI later in
a subsequent definition. Would it not be possible to use PI
here,(since i can't change PI anymore ) ?

Here where?

Pull your definitions out of the class (drop the 'static' keyword, of
course), and they should be working just fine.

Victor
 
M

Mike Wahler

Victor Bazarov said:
Only static data members of integral types can be initialised inside
the class definition. Please in the future don't make us guess or
read your mind. As much as we can do it successfully, it still takes
a lot of energy, which we have a very limited amount to spare.


No, the order of initialisation is well-defined. It's simply not
allowed to initialise a floating point member in the class definition.


Here where?

Pull your definitions out of the class (drop the 'static' keyword, of
course), and they should be working just fine.

Rakesh:

To make sure you understand what Victor means:

Retain the 'static' in your declaration in the class body,
but omit it in the definition, like this:

class C
{
const static float PI;
};

const float C::pI(3.14159);


However, I recommend you change your type to 'double'
which has much greater range and precision, which
will give better accuracy in your floating point
calculations.

-Mike
 
J

Jonathan Mcdougall

Rakesh said:
I am having this code here.


static const float PI = 3.14159;
static const float INC = 0.4f * PI;

When I compile my program, I get the following error,

error: `MyClass::pI' cannot appear in a constant-expression .

It is currently illegal to use non integral
initializers in the class' definition. Define it
elsewhere.

// my_math.h
class Math
{
public:
static const double PI;
};

// my_math.cpp
const double Math::pI = 3.1416;

See 9.4.2.4:
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 (_expr.const_).
I was wondering why this is not possible here.

A quick google search gave

http://groups.google.ca/[email protected]
May be is it because
there is no guaranteed order of initialization among C++ members ?

No, in this case, the order is defined. PI gets
initialized before INC.


Jonathan
 
R

Rakesh Sinha

But specifying 'static const float PI = 3.14159f;' seems to compile
fine on my implementation (gnu c/c++ 3.41 - I know that does not mean
it is legal.) . Hence I did not even think that there is a problem
there. Thanks everyone for pointing out the same.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top