Initialization inside a class

R

Roman

Hi,

Does anyone know why in C++, it is ok to initialize a const static int
inside a class, but you can't initialize a float?

From what I understand, initialization of const static float can only
be done inside a constructor.

For example:

class A
{
public:

const static int b = 123; // OK
const static double c = 123.456; // ERROR

A();
~A();
};


Roman
 
R

red floyd

Roman said:
Hi,

Does anyone know why in C++, it is ok to initialize a const static int
inside a class, but you can't initialize a float?

Because the Standard says so.

From what I understand, initialization of const static float can only
be done inside a constructor.
Incorrect. It needs to be done in the declaration of the static const
member.
For example:

class A
{
public:

const static int b = 123; // OK const static double c;

A();
~A();
};

const double A::c = 123.456;

See, that's not inside a constructor.
 
A

Alf P. Steinbach

* Roman:
Does anyone know why in C++, it is ok to initialize a const static int
inside a class, but you can't initialize a float?

Nobody _knows_ why, but the decision was made to support integral
constants so that they could be used in compile time expressions,
without generalizing the feature.

Generalization could be difficult.

For now, you can do one of two things if you want that value specified
in a header file:

* Place the constant outside the class, with internal linkage.
* Use the template constant trick.

Here's the template constant trick (perhaps I should apply for a
patent?):

template< typename T >
struct StrawberryConstants { static const double pi; };

template< typename T >
double StrawberryConstants<T>::pi = 3.14;

struct Dummy {};

class Strawberry: public StrawberryConstants<Dummy>
{
// Whatever.
};

One may of course argue that the template constant trick means there's
no _technical_ reason why the language can't support that directly.
After all, any C++ compiler is required to be able to deal with it.
Perhaps it's just that these features were introduced in the "wrong"
order, and nobody's yet found it important enough to champion a clean-up
operation vis-a-vis the standardization committee.
 
J

Jim Langston

Alf P. Steinbach said:
* Roman:

Nobody _knows_ why, but the decision was made to support integral
constants so that they could be used in compile time expressions,
without generalizing the feature.

Generalization could be difficult.

For now, you can do one of two things if you want that value specified
in a header file:

* Place the constant outside the class, with internal linkage.
* Use the template constant trick.

Here's the template constant trick (perhaps I should apply for a
patent?):

template< typename T >
struct StrawberryConstants { static const double pi; };

template< typename T >
double StrawberryConstants<T>::pi = 3.14;

struct Dummy {};

I find little benifit of having to delcare Dummy{}; then to just have the
statement
StrawberryConstants::pi = 3.14;
 
J

Jim Langston

Jim Langston said:
I find little benifit of having to delcare Dummy{}; then to just have the
statement
StrawberryConstants::pi = 3.14;

Of course I meant
const double StrawberryConstants::pi = 3.14;
 
A

Alf P. Steinbach

* Jim Langston:
I find little benifit of having to delcare Dummy{}; then to just have the
statement
StrawberryConstants::pi = 3.14;

You don't have to.

Use "int" or whatever, although that will reduce readability by implying
the type matters (some libraries provide a ready-made Dummy type).

Btw. that should be "benefit" (your fault) and "const" (my fault).


Cheers,

- Alf
 
A

Axter

Roman said:
Hi,

Does anyone know why in C++, it is ok to initialize a const static int
inside a class, but you can't initialize a float?

From what I understand, initialization of const static float can only
be done inside a constructor.

For example:

class A
{
public:

const static int b = 123; // OK
const static double c = 123.456; // ERROR

A();
~A();
};

A simple workaround solution is to use an inline static function with a
static local variable.

class A
{
public:
const static int b = 123; // OK
static inline double get_c(){
const static double c = 123.456;
return c;
}
A();
~A();
};
 

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,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top