Initialise static const in class declaration - why only ints?

O

Owen Ransen

When I installed the new VC 2005 I though, aha
at last I will be able initialise static const
members in the H file, and I can, but only
integer types. double types have to be declared
in the H file but initialied in the CPP file.

It seems that MS is following the standard this
time, but why is there that rule?


Easy to use graphics effects:
http://www.ransen.com/
 
V

Victor Bazarov

Owen said:
When I installed the new VC 2005 I though, aha
at last I will be able initialise static const
members in the H file, and I can, but only
integer types. double types have to be declared
in the H file but initialied in the CPP file.

It seems that MS is following the standard this
time, but why is there that rule?

I am not sure why static const members of non-integral types
cannot be initialised in the class definition. You might find
it useful to search the archives of 'comp.std.c++' for that.

It is possible that it has been recognized as a defect and some
work is being done (as we speak) to fix that. But then again,
it is possible that it's not a defect, and the intentions are
simply unclear.

V
 
S

Serge Skorokhodov

Owen said:
When I installed the new VC 2005 I though, aha
at last I will be able initialise static const
members in the H file, and I can, but only
integer types. double types have to be declared
in the H file but initialied in the CPP file.

It seems that MS is following the standard this
time, but why is there that rule?
I'm afraid that it is not only that static constants initialized
within class declaration are to be of an integer type, it is that
such constants are to be initialized with so called simple
integer expression and produce undefined behavior otherwise.
 
O

Owen Ransen

I'm afraid that it is not only that static constants initialized
within class declaration are to be of an integer type, it is that
such constants are to be initialized with so called simple
integer expression and produce undefined behavior otherwise.

Thanks, but I was wondering if anyone knew why that should
be, if someone knew of a web page where the reasoning was explained,
I'm just curious...


Easy to use graphics effects:
http://www.ransen.com/
 
J

James Kanze

When I installed the new VC 2005 I though, aha
at last I will be able initialise static const
members in the H file, and I can, but only
integer types. double types have to be declared
in the H file but initialied in the CPP file.
It seems that MS is following the standard this
time, but why is there that rule?

I think you've got the question backwards. The declaration of a
static member within the class is just that, a declaration, and
not a definition. Thus, normally, you cannot initialize
anything there. There is a special exemption (read: hack) for
integral types, initialized with a constant integral expression,
since there are a few contexts where the compiler requires such
things. Since only constant integral expressions are concerned
by such contexts, the standard limited the hack to types which
can be used directly (without casting) in such expressions.
 
O

Owen Ransen

I think you've got the question backwards. The declaration of a
static member within the class is just that, a declaration, and
not a definition. Thus, normally, you cannot initialize
anything there. There is a special exemption (read: hack) for
integral types, initialized with a constant integral expression,
since there are a few contexts where the compiler requires such
things. Since only constant integral expressions are concerned
by such contexts, the standard limited the hack to types which
can be used directly (without casting) in such expressions.

Thanks for the reply. Its a ha... (oops!) special exemption!


Easy to use graphics effects:
http://www.ransen.com/
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top