Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializing static const members with gcc 4

Reply
Thread Tools

Initializing static const members with gcc 4

 
 
Drew McCormack
Guest
Posts: n/a
 
      06-23-2005
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:rotonMassInAtomicUnits' 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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-23-2005
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:rotonMassInAtomicUnits' 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
 
Reply With Quote
 
 
 
 
Drew McCormack
Guest
Posts: n/a
 
      06-24-2005
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments