Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > error Message: "only const static integral data members can be initializedinside a class or struct"

Reply
Thread Tools

error Message: "only const static integral data members can be initializedinside a class or struct"

 
 
Susan Baker
Guest
Posts: n/a
 
      07-03-2005
Hi,

I got this error msg whilst building some classes. It is realatively asy
to fix. But I just wondered, does anyone know the technical reason why
one can't initialiaze a const static non-integral data member in a class?


 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      07-03-2005
"Susan Baker" <> wrote in message
news:da8j1a$alj$
> Hi,
>
> I got this error msg whilst building some classes. It is realatively
> asy to fix. But I just wondered, does anyone know the technical
> reason why one can't initialiaze a const static non-integral data
> member in a class?


1. Because the C++ standard says so.
2. Because being able to do this with integral types means they can be used
where compile time constants are required, notably in specifying the size of
arrays in the class declaration. There isn't a comparable need for the
initialisation of other const static types.

--
John Carson

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-03-2005
* Susan Baker:
>
> I got this error msg whilst building some classes. It is realatively asy
> to fix. But I just wondered, does anyone know the technical reason why
> one can't initialiaze a const static non-integral data member in a class?


No.

But apparently, from earlier discussions, adding support for that would
have opened a can'o'worms, other related issues to be considered.

The support that is there, for integral type constants, allows those
constants to be used as template arguments and as array size specifiers.
A double (say) constant cannot be used that way. So it seems the
committee chose to add only the minimal support absolutely required for
the most common compile time usage.

Btw., "relatively easy to fix", in the sense of finding some
work-around, isn't necessarily true, because C++ doesn't support
'inline' for constants, i.e. you cannot just use the keyword 'inline' to
make a constant definition have external linkage in a way so the that
the linker is happy with multiple definitions.

One workaround for that is to use templating, and that makes the
restriction very difficult to understand: the templating work-around
shows that it would not entail anything extra in compiler and linker
machinery to support 'inline' for this purpose.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Integral const static member initialized inside a class subramanian100in@yahoo.com, India C++ 5 02-15-2011 07:15 PM
Static const integral data members can be initialized? Immortal Nephi C++ 15 07-23-2010 06:07 PM
Why can't static const float class members be inititalized in the class? Martin Rennix C++ 5 04-18-2007 10:33 AM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Why no non-integral static const initialiser's within class definition? Mike Hewson C++ 14 01-07-2005 08:43 AM



Advertisments