Static const class members, definition and initialization

Discussion in 'C++' started by DanielBradley, Jun 2, 2004.

  1. Hello all,

    I have recently been porting code from Linux to cygwin and came across
    a problem with static const class members (discussed below). I am
    seeking to determine whether I am programming non-standard C++ or if the
    problem lies elsewhere.

    To summarize static const class members are not being accessed properly
    when accessed from a DLL by another external object file (not within the
    DLL). It only occurs when the static const members are initialized
    within the source (.cpp) file instead of within the class declaration.
    Non-const members however seem to work fine, and the member seems to be
    fine when accessed within the DLL.

    I've googled on "static const" and searched the cygwin mailing lists,
    but could not find any related discussions.

    I've also scoured a draft C++ standard that I found on the web [ISO C++]
    and though I found the section that describes initialization of static
    members, it did not seem to mention any difference caused by const.

    The main question that I want to find answer to is:

    Must static const members be initialized within the class
    declaration?, or can they also be initialized within the class
    implementation?

    Followed is an example:

    A.dll
    A.h:
    class A
    {
    public:
    static const int cmember;
    static int member;
    }

    A.cpp:
    const int A::cmember = 10;
    int A::member = 15;

    main.cpp
    #include <A.h>
    #include <cstdio>

    fprintf( stdout, "Value of A::cmember %i\n", A::cmember );
    fprintf( stdout, "Value of A::member %i\n", A::member );

    Below is the output from a similar program - compiled and run under
    cygwin - that has 3 classes A, B and C, and the main function. Class A
    and B are each within their own DLL. Class C is linked at compile time
    with main to produce the executable. BTW The same code compiled (as .so
    libraries) and run under Linux runs as (I) expected.

    A's Members via B
    cmember: 1359750655
    member: 15


    A's Members straight from A
    cmember: 1098655231
    member: 15


    A's Members via A's static methods
    cmember: 10
    member: 15


    A's Members via A's instance methods
    cmember: 10
    member: 15


    C's Members straight from C
    cmember: 20
    member: 30


    A's Members via B (2nd try)
    cmember: 1359750655
    member: 15


    A's Members straight from A (2nd try)
    cmember: 1098655231
    member: 15

    Thank you for time,
    Cheers,
    Daniel Bradley

    [ISO C++]
    Initialization of non-local objects (section 3.6.2)
    Working Paper for Draft Proposed International Standard for Information
    Systems - Programming Language C++
    http://www.open-std.org/jtc1/sc22/open/n2356/
    DanielBradley, Jun 2, 2004
    #1
    1. Advertising

  2. "DanielBradley" <> wrote...
    > I have recently been porting code from Linux to cygwin and came across
    > a problem with static const class members (discussed below). I am
    > seeking to determine whether I am programming non-standard C++ or if the
    > problem lies elsewhere.
    >
    > To summarize static const class members are not being accessed properly
    > when accessed from a DLL by another external object file (not within the
    > DLL). It only occurs when the static const members are initialized
    > within the source (.cpp) file instead of within the class declaration.
    > Non-const members however seem to work fine, and the member seems to be
    > fine when accessed within the DLL.


    Just to let you know, DLLs are not specified by the language definition,
    so anything particular to them is not on topic of this newsgroup. Just
    to let you know...

    > I've googled on "static const" and searched the cygwin mailing lists,
    > but could not find any related discussions.
    >
    > I've also scoured a draft C++ standard that I found on the web [ISO C++]
    > and though I found the section that describes initialization of static
    > members, it did not seem to mention any difference caused by const.


    There shouldn't be any difference.

    > The main question that I want to find answer to is:
    >
    > Must static const members be initialized within the class
    > declaration?


    Must? No. Can? Yes, but only if they are of integral type. If
    they are not integral (enum, int, char), they must NOT be initailised
    in the class definition (not declaration). In addition to that, even
    if a const _is_ initialised in the class definition, it still has to
    be defined _outside_ the class definition if it's used in the program
    (outside the class definition, that is).

    >, or can they also be initialized within the class
    > implementation?


    There can be only one _initialisation_. However, if you remember
    that _any_ static data members need to be defined and initialised
    _outside_ the class definition, you cannot go wrong.

    >
    > Followed is an example:


    Apparently, you typed it in right here instead of posting real
    and compileable code. Try to avoid doing that in the future.

    >
    > A.dll
    > A.h:
    > class A
    > {
    > public:
    > static const int cmember;
    > static int member;
    > }


    ;

    >
    > A.cpp:


    #include <A.h>

    > const int A::cmember = 10;
    > int A::member = 15;
    >
    > main.cpp
    > #include <A.h>
    > #include <cstdio>
    >
    > fprintf( stdout, "Value of A::cmember %i\n", A::cmember );
    > fprintf( stdout, "Value of A::member %i\n", A::member );


    This is not a valid C++ program.

    >
    > Below is the output from a similar program - compiled and run under
    > cygwin - that has 3 classes A, B and C, and the main function. Class A
    > and B are each within their own DLL. Class C is linked at compile time
    > with main to produce the executable. BTW The same code compiled (as .so
    > libraries) and run under Linux runs as (I) expected.
    >
    > A's Members via B


    What does "via B" mean?

    > cmember: 1359750655
    > member: 15
    >
    >
    > A's Members straight from A
    > cmember: 1098655231
    > member: 15
    >
    >
    > A's Members via A's static methods
    > cmember: 10
    > member: 15
    >
    >
    > A's Members via A's instance methods
    > cmember: 10
    > member: 15
    >
    >
    > C's Members straight from C
    > cmember: 20
    > member: 30
    >
    >
    > A's Members via B (2nd try)
    > cmember: 1359750655
    > member: 15
    >
    >
    > A's Members straight from A (2nd try)
    > cmember: 1098655231
    > member: 15


    So, you have some kind of a problem of either _initialisation_ or
    _non-shared_ data segments. You should really seek advice from
    a compiler newsgroup. See the list of suggested newsgroups in the
    'Welcome' message posted here weekly.

    Victor
    Victor Bazarov, Jun 2, 2004
    #2
    1. Advertising

  3. Victor Bazarov wrote:

    Victor, thank you for your response. It would seem that I am writing
    standards compliant C++ and should look for more answers elsewhere.

    I think the next thing to do is to test the code using a different
    Windows compiler to see if the issue is specific to cygwin or not. As it
    seems the C++ is correct I'll also post a message to the cygwin mailing
    list hopefully they will be interested regardless.

    I'll post a follow up here when I find the problem.

    I have answered your questions below.

    Thanks,
    Daniel.

    > "DanielBradley" <> wrote...
    >
    >>I have recently been porting code from Linux to cygwin and came across
    >>a problem with static const class members (discussed below). I am
    >>seeking to determine whether I am programming non-standard C++ or if the
    >>problem lies elsewhere.
    >>
    >>To summarize static const class members are not being accessed properly
    >>when accessed from a DLL by another external object file (not within the
    >>DLL). It only occurs when the static const members are initialized
    >>within the source (.cpp) file instead of within the class declaration.
    >>Non-const members however seem to work fine, and the member seems to be
    >>fine when accessed within the DLL.

    >
    >
    > Just to let you know, DLLs are not specified by the language definition,
    > so anything particular to them is not on topic of this newsgroup. Just
    > to let you know...
    >
    >
    >>I've googled on "static const" and searched the cygwin mailing lists,
    >>but could not find any related discussions.
    >>
    >>I've also scoured a draft C++ standard that I found on the web [ISO C++]
    >>and though I found the section that describes initialization of static
    >>members, it did not seem to mention any difference caused by const.

    >
    >
    > There shouldn't be any difference.
    >
    >
    >>The main question that I want to find answer to is:
    >>
    >>Must static const members be initialized within the class
    >>declaration?

    >
    >
    > Must? No. Can? Yes, but only if they are of integral type. If
    > they are not integral (enum, int, char), they must NOT be initailised
    > in the class definition (not declaration). In addition to that, even
    > if a const _is_ initialised in the class definition, it still has to
    > be defined _outside_ the class definition if it's used in the program
    > (outside the class definition, that is).
    >
    >
    >>, or can they also be initialized within the class
    >>implementation?

    >
    >
    > There can be only one _initialisation_. However, if you remember
    > that _any_ static data members need to be defined and initialised
    > _outside_ the class definition, you cannot go wrong.
    >
    >
    >>Followed is an example:

    >
    >
    > Apparently, you typed it in right here instead of posting real
    > and compileable code. Try to avoid doing that in the future.


    Hmmm, sorry about that, was actually type copied from my code, just
    forgot the important parts of main :) Should have copied and pasted.

    >
    >>A.dll
    >>A.h:
    >>class A
    >>{
    >>public:
    >>static const int cmember;
    >>static int member;
    >>}

    >
    >
    > ;
    >
    >
    >>A.cpp:

    >
    >
    > #include <A.h>
    >
    >>const int A::cmember = 10;
    >>int A::member = 15;
    >>
    >>main.cpp
    >>#include <A.h>
    >>#include <cstdio>
    >>
    >>fprintf( stdout, "Value of A::cmember %i\n", A::cmember );
    >>fprintf( stdout, "Value of A::member %i\n", A::member );

    >
    >
    > This is not a valid C++ program.
    >
    >
    >>Below is the output from a similar program - compiled and run under
    >>cygwin - that has 3 classes A, B and C, and the main function. Class A
    >>and B are each within their own DLL. Class C is linked at compile time
    >>with main to produce the executable. BTW The same code compiled (as .so
    >>libraries) and run under Linux runs as (I) expected.
    >>
    >>A's Members via B

    >
    >
    > What does "via B" mean?


    Accessed from A via a instance method in B.

    #include "B.h"


    B.cpp________________________________

    #include <A.h>


    int
    B::getConstMemberOfA()
    {
    return A::cmember;
    }


    int
    B::getMemberOfA()
    {
    return A::member;
    }
    ____________________________________

    >
    >>cmember: 1359750655
    >>member: 15
    >>
    >>
    >>A's Members straight from A
    >>cmember: 1098655231
    >>member: 15
    >>
    >>
    >>A's Members via A's static methods
    >>cmember: 10
    >>member: 15
    >>
    >>
    >>A's Members via A's instance methods
    >>cmember: 10
    >>member: 15
    >>
    >>
    >>C's Members straight from C
    >>cmember: 20
    >>member: 30
    >>
    >>
    >>A's Members via B (2nd try)
    >>cmember: 1359750655
    >>member: 15
    >>
    >>
    >>A's Members straight from A (2nd try)
    >>cmember: 1098655231
    >>member: 15

    >
    >
    > So, you have some kind of a problem of either _initialisation_ or
    > _non-shared_ data segments. You should really seek advice from
    > a compiler newsgroup. See the list of suggested newsgroups in the
    > 'Welcome' message posted here weekly.
    >
    > Victor
    >
    >
    DanielBradley, Jun 2, 2004
    #3
  4. DanielBradley

    Dave Moore Guest

    DanielBradley <> wrote in message news:<>...
    > Hello all,
    >
    > I have recently been porting code from Linux to cygwin and came across
    > a problem with static const class members (discussed below). I am
    > seeking to determine whether I am programming non-standard C++ or if the
    > problem lies elsewhere.
    >
    > To summarize static const class members are not being accessed properly
    > when accessed from a DLL by another external object file (not within the
    > DLL). It only occurs when the static const members are initialized
    > within the source (.cpp) file instead of within the class declaration.
    > Non-const members however seem to work fine, and the member seems to be
    > fine when accessed within the DLL.
    >
    > I've googled on "static const" and searched the cygwin mailing lists,
    > but could not find any related discussions.
    >
    > I've also scoured a draft C++ standard that I found on the web [ISO C++]
    > and though I found the section that describes initialization of static
    > members, it did not seem to mention any difference caused by const.
    >
    > The main question that I want to find answer to is:
    >
    > Must static const members be initialized within the class
    > declaration?,


    No, in fact they cannot in general be initialized here, because then
    you would run the risk of having multiple instances of the same
    variable at link-time.

    > or can they also be initialized within the class
    > implementation?
    >


    This is the usual way of doing it, but there are certain restrictions,
    as detailed below.

    From reading your example, I think there is probably more to your
    example than you are stating. Because you are dealing with multiple
    translation/compilation units, I suspect you are having an order of
    initialization problem with your static variables. I recently
    participated in a fairly extensive discussion on exactly this topic in
    comp.lang.c++.moderated which you may find useful:
    http://groups.google.com/groups?hl=

    In any case, the bottom line is that there are absolutely NO
    guarantees about order of initialization of static data (const or
    otherwise) across compilation units. All that you can know for sure
    (according to the Standard) is that static member data is initialized
    EITHER:

    1) when program flow enters main

    OR in the specific case that initialization is deferred until after
    main is entered

    2) before it is used for the first time.

    AND that within a translation unit, static members are initialized in
    the order that their initialization statements are encountered.

    Note that the fact that your static data members are const is
    immaterial .. what matters is whether or not they are statically
    (compile-time) or dynamically (run-time) initialized. Clearly the
    second must be true in your case or else you would not be having these
    problems.

    So, you should avoid creating dependencies between static data members
    in different compilation units. OTOH, if you have no such dependecies
    in your code and the test cases are still producing screwy results,
    then I suspect your compiler may be at fault. Note that there may be
    some sort of "pragma hack" (i.e. non-Standard) provided by compiler
    vendors to deal with this sort of issue.

    Finally, the fact that you are using .DLL's (which I have little
    experience with) may also complicate matters. There is another thread
    currently under discussion on comp.lang.c++.moderated that you may
    find useful, although it is not directly related to static member
    data, there is a lot of information about .DLL's and how objects are
    distributed across them.
    http://groups.google.com/groups?q=

    HTH, Dave Moore


    [example snipped]
    Dave Moore, Jun 2, 2004
    #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. JFCM
    Replies:
    4
    Views:
    5,732
  2. Rakesh Sinha
    Replies:
    4
    Views:
    1,847
    Rakesh Sinha
    Jan 13, 2005
  3. Replies:
    3
    Views:
    377
    Old Wolf
    Oct 10, 2006
  4. Martin Rennix
    Replies:
    5
    Views:
    345
    Ron Natalie
    Apr 18, 2007
  5. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
Loading...

Share This Page