Initializing static const members with gcc 4

D

Drew McCormack

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
 
V

Victor Bazarov

Drew said:
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
 
D

Drew McCormack

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top