Why static const string = "NA"; is not allowed inside the class.

Discussion in 'C++' started by Thinking_In_CPP, Feb 13, 2008.

  1. Hi All,

    I have a query

    Why static const string = "NA"; is not allowed inside the class when
    the standard allows

    static const int = 5; inside the class

    Please Explain.
    Thinking_In_CPP, Feb 13, 2008
    #1
    1. Advertising

  2. Thinking_In_CPP

    Martin York Guest

    On Feb 12, 4:14 pm, Thinking_In_CPP <> wrote:
    > Hi All,
    >
    > I have a query
    >
    > Why static const string = "NA"; is not allowed inside the class when
    > the standard allows
    >
    > static const int = 5; inside the class
    >
    > Please Explain.



    Because converting "NA" which is "const char * const" (or something
    similar) in-to a std::string requires you to call the constructor.

    While 'static const int' members can be optimized completely out of
    the code and not even require storage space (if you are lucky). This
    special case was recognized as a an easy optimization and added to the
    language for this reason.
    Martin York, Feb 13, 2008
    #2
    1. Advertising

  3. Thinking_In_CPP

    Jeff Schwab Guest

    Thinking_In_CPP wrote:
    > Hi All,
    >
    > I have a query
    >
    > Why static const string = "NA"; is not allowed inside the class when
    > the standard allows
    >
    > static const int = 5; inside the class
    >
    > Please Explain.


    http://www.research.att.com/~bs/bs_faq2.html#in-class

    "So why do these inconvenient restrictions exist? A class is typically
    declared in a header file and a header file is typically included into
    many translation units. However, to avoid complicated linker rules, C++
    requires that every object has a unique definition. That rule would be
    broken if C++ allowed in-class definition of entities that needed to be
    stored in memory as objects. See D&E for an explanation of C++'s design
    tradeoffs."
    Jeff Schwab, Feb 13, 2008
    #3
  4. Thinking_In_CPP

    James Kanze Guest

    On Feb 13, 1:14 am, Thinking_In_CPP <> wrote:

    > I have a query


    > Why static const string = "NA"; is not allowed inside the class when
    > the standard allows


    > static const int = 5; inside the class


    Within the class, the declaration of a static member is not a
    definition, but a declaration, and declarations can't have
    initializers. There's a special exception (aka hack) for
    integral types (and only integral types) because constant
    integral expressions place a special role in the language, and
    in order for an integral constant to be treated as a constant
    integral expression, its initializer must be seen.

    In sum, it's allowing the initialization for integral types
    which is the anomalous exception, not forbidding it for other
    types.

    --
    James Kanze (GABI Software) email:
    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
    James Kanze, Feb 13, 2008
    #4
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,739
    Smokey Grindel
    Dec 2, 2006
  2. Simon
    Replies:
    10
    Views:
    617
    mlimber
    May 9, 2006
  3. John Goche
    Replies:
    5
    Views:
    646
    Victor Bazarov
    Dec 8, 2006
  4. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  5. Iñaki Baz Castillo
    Replies:
    13
    Views:
    476
    Iñaki Baz Castillo
    May 1, 2011
Loading...

Share This Page