C++11 allows static member initialisation

Discussion in 'C++' started by Single Stage to Orbit, Aug 5, 2012.

  1. I have this:

    $ more static-initialisers.cpp
    class Simple
    {
    static int mCounter = 0;
    };
    $ g++-4.6.3 -std=c++0x -c static-initialisers.cpp
    static-initialisers.cpp:3:24: error: ISO C++ forbids in-class
    initialization of non-const static member ‘mCounter’

    Which version of G++ allows this? The docs for 4.7.x doesn't say if it
    is allowed.

    I know I can simply do:

    int Simple::mCounter = 0

    to get around this though.
    --
    Tactical Nuclear Kittens
     
    Single Stage to Orbit, Aug 5, 2012
    #1
    1. Advertising

  2. Single Stage to Orbit

    Luca Risolia Guest

    On 06/08/2012 00:11, Single Stage to Orbit wrote:
    > I have this:
    >
    > $ more static-initialisers.cpp
    > class Simple
    > {
    > static int mCounter = 0;
    > };
    > $ g++-4.6.3 -std=c++0x -c static-initialisers.cpp
    > static-initialisers.cpp:3:24: error: ISO C++ forbids in-class
    > initialization of non-const static member ‘mCounter’
    >
    > Which version of G++ allows this?


    None probably.

    > The docs for 4.7.x doesn't say if it is allowed.


    With --std=C++0x, GCC obeys to the C++11 standard (§ 9.4.2):

    "
    The definition for a static data member shall appear in a namespace
    scope enclosing the member’s class definition. In the definition at
    namespace scope, the name of the static data member shall be qualified
    by its class name using the :: operator. The initializer expression in
    the definition of a static data member is in the scope of its class
    (3.3.7).

    [ Example:

    class process {
    static process* run_chain;
    static process* running;
    };
    process* process::running = get_main();
    process* process::run_chain = running;
    "
     
    Luca Risolia, Aug 6, 2012
    #2
    1. Advertising

  3. Single Stage to Orbit <> wrote:
    > class Simple
    > {
    > static int mCounter = 0;
    > };
    > $ g++-4.6.3 -std=c++0x -c static-initialisers.cpp
    > static-initialisers.cpp:3:24: error: ISO C++ forbids in-class
    > initialization of non-const static member ???mCounter???
    >
    > Which version of G++ allows this? The docs for 4.7.x doesn't say if it
    > is allowed.


    Why do you assume that it's allowed?

    I think you are confusing it with (non-static) in-class member
    initialization. That's a different thing. (It's basically a shortcut
    to writing the initialization into the constructor of the class. Static
    variables are not initialized in the constructor.)
     
    Juha Nieminen, Aug 6, 2012
    #3
  4. On Mon, 2012-08-06 at 07:24 +0000, Juha Nieminen wrote:
    > Single Stage to Orbit <> wrote:
    > > class Simple
    > > {
    > > static int mCounter = 0;
    > > };
    > > $ g++-4.6.3 -std=c++0x -c static-initialisers.cpp
    > > static-initialisers.cpp:3:24: error: ISO C++ forbids in-class
    > > initialization of non-const static member ???mCounter???
    > >
    > > Which version of G++ allows this? The docs for 4.7.x doesn't say if

    > it
    > > is allowed.

    >
    > Why do you assume that it's allowed?


    Because "Professional C++ 2nd Edition" says that with C++11 I can do
    this. Evidently it is wrong.

    > I think you are confusing it with (non-static) in-class member
    > initialization. That's a different thing. (It's basically a shortcut
    > to writing the initialization into the constructor of the class.
    > Static variables are not initialized in the constructor.)


    Or has the standard changed to disallow static in-class member
    initialization?
    --
    Tactical Nuclear Kittens
     
    Single Stage to Orbit, Aug 6, 2012
    #4
  5. Paavo Helde <> wrote:
    > Only const static data members can be initialized in-class. For a mutable
    > data member the identity of the variable is important, so it must be placed
    > in a specific compilation unit.


    Btw, at some point in the distant past, gcc (a really old version of it,
    obviously) demanded that even const static class variables be defined in
    a compilation unit. (Ostensibly so you can create pointers pointing to
    that variable.)

    What exactly is the current official status of this? Does the standard
    explicitly say that const static class variables do not need to be
    defined in a compilation unit? Why was gcc demanding it at one point?
    (Was this a concept before the 98 standard came out? Was it in some
    standard draft? Was it in the standard but got changed? Was this simply
    something gcc-exclusive?)

    What happens if you take the address of a const static class variable?
    (Error? Malformed? Implementation-defined? UB? Must have definition in
    this case? Something else?)
     
    Juha Nieminen, Aug 6, 2012
    #5
  6. Paavo Helde <> wrote:
    > The compiler cannot guess in which compilation unit it should appear.


    It doesn't seem to be a problem with static variables in a templated
    class...
     
    Juha Nieminen, Aug 6, 2012
    #6
  7. Single Stage to Orbit <> wrote:
    >> I think you are confusing it with (non-static) in-class member
    >> initialization. That's a different thing. (It's basically a shortcut
    >> to writing the initialization into the constructor of the class.
    >> Static variables are not initialized in the constructor.)

    >
    > Or has the standard changed to disallow static in-class member
    > initialization?


    Does the new standard really say that you can now initialize static
    members in their declaration?

    Note that non-static member initialization in their declaration is
    not *actual* initialization per se. It's just a shorthand that can be used
    instead of writing the actual initialization in the constructor of the class.
    (Note that when you write "int i = 5;" as class member, that variable
    might not actually be initialized with the value 5. A constructor my
    override that initialization with something else in its initializer
    list. The initialization to 5 will be autogenerated only if there's
    no explicit initialization in the initialization list of the constructor.)

    Static members are not initialized in constructors, and hence it's
    a completely different situation.
     
    Juha Nieminen, Aug 6, 2012
    #7
  8. On 06.08.2012 10:38, Juha Nieminen wrote:
    > Paavo Helde <> wrote:
    >> Only const static data members can be initialized in-class. For a mutable
    >> data member the identity of the variable is important, so it must be placed
    >> in a specific compilation unit.

    >
    > Btw, at some point in the distant past, gcc (a really old version of it,
    > obviously) demanded that even const static class variables be defined in
    > a compilation unit. (Ostensibly so you can create pointers pointing to
    > that variable.)
    >
    > What exactly is the current official status of this? Does the standard
    > explicitly say that const static class variables do not need to be
    > defined in a compilation unit? Why was gcc demanding it at one point?
    > (Was this a concept before the 98 standard came out? Was it in some
    > standard draft? Was it in the standard but got changed? Was this simply
    > something gcc-exclusive?)


    It's still a bit fuzzy, but the intent appears to be that if the address
    is never required, then it's A OK to not have definition.

    As I recall it's still specified in the One Definition Rule.


    > What happens if you take the address of a const static class variable?


    Then you need a definition.

    If you want such a definition in a header, it can be placed in base
    class template.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Aug 6, 2012
    #8
  9. On 8/6/2012 5:07 AM, Juha Nieminen wrote:
    > Single Stage to Orbit <> wrote:
    >>> I think you are confusing it with (non-static) in-class member
    >>> initialization. That's a different thing. (It's basically a shortcut
    >>> to writing the initialization into the constructor of the class.
    >>> Static variables are not initialized in the constructor.)

    >>
    >> Or has the standard changed to disallow static in-class member
    >> initialization?

    >
    > Does the new standard really say that you can now initialize static
    > members in their declaration?


    Just to remind the OP, const static members are allowed to be
    initialised in the class definition, and that hasn't changed in the new
    Standard; *some* compilers *extend* this behavior to include non-const
    static data members, but let's emphasize that it is *an extension*.

    > [..]


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 6, 2012
    #9
  10. Paavo Helde <> wrote:
    > Juha Nieminen <> wrote in news:jvnvtb$vg4$2
    > @speranza.aioe.org:
    >
    >> Paavo Helde <> wrote:
    >>> The compiler cannot guess in which compilation unit it should appear.

    >>
    >> It doesn't seem to be a problem with static variables in a templated
    >> class...
    >>

    >
    > Experimentation shows otherwise:
    >
    > template<typename T>
    > struct A {
    > static int bb;
    > void f() {bb=42;}
    > };
    >
    >
    > int main() {
    > A<int> x;
    > x.f();
    > }


    I didn't say that you don't need to define the static variable. I meant
    to say that you can define it right there in the header, and it doesn't
    seem to cause any problem for the compiler to decide "in which compilation
    unit it should appear"

    My point was that the argument that the compiler needs to know the exact
    compilation unit where to put the static variable is not valid because
    templated classes demonstrate otherwise.
     
    Juha Nieminen, Aug 6, 2012
    #10
    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. Samuele Armondi
    Replies:
    4
    Views:
    1,034
    Victor Bazarov
    Jun 25, 2003
  2. Tim
    Replies:
    2
    Views:
    1,510
  3. Tim Clacy
    Replies:
    8
    Views:
    399
    Tim Clacy
    May 30, 2006
  4. dolphin
    Replies:
    3
    Views:
    1,354
    Pete Becker
    Dec 5, 2007
  5. Sanatan
    Replies:
    4
    Views:
    478
    Sanatan
    Dec 28, 2010
Loading...

Share This Page