Structure of Constants

J

jsnX

Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double r = 0.6435011087932843868;
static const double a = 0.2;

virtual void dummy() = 0;
};
}
=====
==========
however, this example fails with the following errors:
==========
=====
error: ISO C++ forbids initialization of member constant `w' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `r' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `a' of
non-integral type `const double'
=====
==========
this really seems like it should work - is it a fact that I can not
initialize non-integer constants? yikes!!
 
M

Malte Starostik

jsnX said:
Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double r = 0.6435011087932843868;
static const double a = 0.2;

virtual void dummy() = 0;
};
}
Your question is about a namespace, but the constants are inside a struct.
=====
==========
however, this example fails with the following errors:
==========
=====
error: ISO C++ forbids initialization of member constant `w' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `r' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `a' of
non-integral type `const double'
=====
==========
this really seems like it should work - is it a fact that I can not
initialize non-integer constants? yikes!!

Not inside a class (or struct) definition. You can move the constants
out of struct defaults and into namespace scope. Or you can declare
them as static const members and define them outside the class
definition like any static member. However that will deprive the
compiler of any chance to optimise by using compile-time constants in
any translation unit other than the one that defines them.

Cheers,
Malte
 
S

Sumit Rajan

jsnX said:
Say I would like my namespace to have some constants associated with
it, like this:
==========
=====
namespace broadpen
{
struct defaults
{
static const double w = 10.0;
static const double r = 0.6435011087932843868;
static const double a = 0.2;

virtual void dummy() = 0;
};
}
=====
==========
however, this example fails with the following errors:
==========
=====
error: ISO C++ forbids initialization of member constant `w' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `r' of
non-integral type `const double'
error: ISO C++ forbids initialization of member constant `a' of
non-integral type `const double'
=====

namespace broadpen
{
struct defaults
{
static const double w;
static const double r;
static const double a;

virtual void dummy() = 0;
};
}


and something like this will go into your implementation file:

const double broadpen::defaults::w = 10.0;
const double broadpen::defaults::r = 0.6435011087932843868;
const double broadpen::defaults::a = 0.2;

==========
this really seems like it should work - is it a fact that I can not
initialize non-integer constants? yikes!!

Yes. Not in the class definition.

Regards,
Sumit.
 
J

jsnX

would adding 'static' to this code do anything good, performance wise?
or would it just restrict these constants to the file they were in?

namespace broadpen
{
namespace defaults
{
const double w = 10.0;
const double r = 0.6435011087932843868;
const double a = 0.2;
}
}
 
M

Malte Starostik

jsnX said:
would adding 'static' to this code do anything good, performance wise?
or would it just restrict these constants to the file they were in?

namespace broadpen
{
namespace defaults
{
const double w = 10.0;
const double r = 0.6435011087932843868;
const double a = 0.2;
}
}

It wouldn't do any good, I'm not even sure if it's allowed inside a
namespace other than the global one (and if so, it's deprecated).
The above code is the way to go for constants that are not of integral type.

Cheers,
Malte
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top