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

Discussion in 'C++' started by Susan Baker, Jul 3, 2005.

  1. Susan Baker

    Susan Baker Guest

    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?
     
    Susan Baker, Jul 3, 2005
    #1
    1. Advertising

  2. Susan Baker

    John Carson Guest

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

    "Susan Baker" <> wrote in message
    news:da8j1a$alj$-infra.bt.com
    > 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
     
    John Carson, Jul 3, 2005
    #2
    1. Advertising

  3. Re: error Message: "only const static integral data members can be initialized inside a class or struct"

    * 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?
     
    Alf P. Steinbach, Jul 3, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWc=?=
    Replies:
    1
    Views:
    2,200
    Marshal Antony
    Feb 8, 2004
  2. =?Utf-8?B?bWc=?=

    Size of e-mail message - message.Body = msgText

    =?Utf-8?B?bWc=?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    600
    Chad Z. Hower aka Kudzu
    Feb 11, 2004
  3. - Steve -
    Replies:
    0
    Views:
    730
    - Steve -
    Jul 6, 2004
  4. tshad
    Replies:
    0
    Views:
    826
    tshad
    Jan 27, 2005
  5. Hari Om
    Replies:
    3
    Views:
    12,286
    Wendy S
    Oct 1, 2003
Loading...

Share This Page