Initializing static const members with gcc 4

Discussion in 'C++' started by Drew McCormack, Jun 23, 2005.

  1. I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I
    have a header with the following const variables with namespace scope:

    namespace Periphery {

    extern const double ProtonMassInAtomicUnits = 1836.152755656068;

    }

    I try to use these in another header to initialize static const member
    variables, like so:

    namespace Periphery {

    class RMatrixH2Pt3DSolver {

    static const double hMass = ProtonMassInAtomicUnits;

    I get this error:

    /Volumes/BigDisk/Develop/periphery/src/RMatrixH2Pt3DSolver.h:40: error:
    'Periphery::protonMassInAtomicUnits' cannot appear in a
    constant-expression

    I assume that g++ 4 is doing the right thing, adhering more closely to
    the standard than 3.4. But my question is: how can I define a constant
    namespace variable like ProtonMassInAtomicUnits, and use that to
    initialize static constants like hMass? Do I have to use the
    preprocessor and define a macro?

    I prefer not to do that because then I can't put the constant in the
    Periphery namespace.

    Drew McCormack
     
    Drew McCormack, Jun 23, 2005
    #1
    1. Advertising

  2. Drew McCormack wrote:
    > I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have
    > a header with the following const variables with namespace scope:
    >
    > namespace Periphery {
    >
    > extern const double ProtonMassInAtomicUnits = 1836.152755656068;
    >
    > }
    >
    > I try to use these in another header to initialize static const member
    > variables, like so:
    >
    > namespace Periphery {
    >
    > class RMatrixH2Pt3DSolver {
    >
    > static const double hMass = ProtonMassInAtomicUnits;


    Static const data members can only be initialised inside the class if
    they are of an integral type. Doubles have to be initialized _outside_,
    where they are defined.

    >
    > I get this error:
    >
    > /Volumes/BigDisk/Develop/periphery/src/RMatrixH2Pt3DSolver.h:40: error:
    > 'Periphery::protonMassInAtomicUnits' cannot appear in a constant-expression


    I don't know about this. It shouldn't give you this problem. The issue,
    however, is that you're not allowed to initialise static const doubles in
    the class.

    >
    > I assume that g++ 4 is doing the right thing, adhering more closely to
    > the standard than 3.4. But my question is: how can I define a constant
    > namespace variable like ProtonMassInAtomicUnits, and use that to
    > initialize static constants like hMass? Do I have to use the
    > preprocessor and define a macro?


    If you're using compiler extensions that allow you to initialise static
    constants of a non-integral type inside the class definition, try to use
    another compile-time const in both places:

    const double PMIAU = 1836.152755656068; // compile-time constant

    namespace Periphery {
    extern const double ProtonMassInAtomicUnits = PMIAU;
    }
    ...
    namespace Periphery {
    class RMatrixH2Pt3DSolver {
    static const double hMass = PMIAU;

    However, you should still know that this initialising 'hMass' is not
    portable, since it is not allowed by the Standard.

    > I prefer not to do that because then I can't put the constant in the
    > Periphery namespace.


    V
     
    Victor Bazarov, Jun 23, 2005
    #2
    1. Advertising

  3. >
    > However, you should still know that this initialising 'hMass' is not
    > portable, since it is not allowed by the Standard.


    Thank you Victor. This is what I needed to know.

    Regards,
    Drew
     
    Drew McCormack, Jun 24, 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. JFCM
    Replies:
    4
    Views:
    5,739
  2. mark fine
    Replies:
    1
    Views:
    469
    Michael Jørgensen
    Nov 9, 2004
  3. Rakesh Sinha
    Replies:
    4
    Views:
    1,852
    Rakesh Sinha
    Jan 13, 2005
  4. Madhav
    Replies:
    14
    Views:
    555
    Howard
    Jul 25, 2006
  5. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
Loading...

Share This Page