470,596 Members | 1,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

Initializing static const members with gcc 4

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

Jul 23 '05 #1
2 3408
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
Jul 23 '05 #2
>
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

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by trying_to_learn | last post: by
5 posts views Thread by john smith | last post: by
2 posts views Thread by Daniel Switkin | last post: by
3 posts views Thread by Diebels | last post: by
10 posts views Thread by sunil | last post: by
9 posts views Thread by Christopher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.