By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,363 Members | 3,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,363 IT Pros & Developers. It's quick & easy.

Initialise static const in class declaration - why only ints?

P: n/a
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/
Apr 26 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Owen Ransen wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 26 '07 #2

P: n/a
Owen Ransen wrote:
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.

--
Serge

Apr 26 '07 #3

P: n/a
On Thu, 26 Apr 2007 23:03:55 +0400, Serge Skorokhodov
<ri*******@gmail.comwrote:
>Owen Ransen wrote:
>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.
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/
Apr 27 '07 #4

P: n/a
On Apr 26, 7:14 pm, Owen Ransen <w...@wonker.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 27 '07 #5

P: n/a
On 27 Apr 2007 05:22:00 -0700, James Kanze <ja*********@gmail.com>
wrote:
>On Apr 26, 7:14 pm, Owen Ransen <w...@wonker.comwrote:
>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.
Thanks for the reply. Its a ha... (oops!) special exemption!
Easy to use graphics effects:
http://www.ransen.com/
Apr 27 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.