Constants and namespaces

R

Rune Allnor

Hi folks.

I naively tried to define some useful constants inside a namespace:

namespace me{
#define PI = 3.14;
};

It works when I refer to the constant from within that namespace,
but from the outside I get compiler errors:

float a= me::pI * 28;

One obvious solution is to define the constant in the global
namespace, but I would prefer not to. So I tried a simple
static variable:

namespace me{
template<class T>
static const T Pi = 3.14;
};

but that, too, gives a compiler error.

So how does one solve this simple task?

Rune
 
F

Fred Zwarts

Rune Allnor said:
Hi folks.

I naively tried to define some useful constants inside a namespace:

namespace me{
#define PI = 3.14;
};

You do not define a constant. You define a macro (which is not bound to the namespace scope).
The macro PI has the value "= 3.14;".
Why don't you define a constant in the normal way:

const float Pi = 3.14;
It works when I refer to the constant from within that namespace,

I don't think so. Can you give an example where it works correctly?
The perprocessor will change e.g.

MyFloat = PI;

to

MYFloat = = 3.14;;

which may be legal C++ code, but probably not what you want.
but from the outside I get compiler errors:

float a= me::pI * 28;

The preprocessor changes this to

float a=me::= 3.14; * 28;

which correctly triggers an error message.
 
F

Fred Zwarts

Clicked the Send button too fast.
The perprocessor will change e.g.

MyFloat = PI;

to

MYFloat = = 3.14;;

which may be legal C++ code, but probably not what you want.


should be:

The preprocessor will change e.g.

MyFloat=PI;

to

MYFloat== 3.14;;

which may be legal C++ code, but probably not what you want.
 
Ä

书呆彭

Rune Allnor 写é“:
Hi folks.

I naively tried to define some useful constants inside a namespace:

namespace me{
#define PI = 3.14;
};

It works when I refer to the constant from within that namespace,
but from the outside I get compiler errors:

float a= me::pI * 28;

One obvious solution is to define the constant in the global
namespace, but I would prefer not to. So I tried a simple
static variable:

namespace me{
template<class T>
static const T Pi = 3.14;
};

but that, too, gives a compiler error.

So how does one solve this simple task?

Rune

i guess you dont know clearly what the preprocessor do and what the compiler do.

the "#define" ,"#if", etc. commands are preprocessor commands,which are used by the preprocessor, not the compiler.
so the #define command dont have a scope,whether a namespace or class or something,the preprocessor just replace it.

to limit the scope of one const,you should use the compiler keyword "const",if this does not work for your compiler,
one alternative is to use "enum const". for instance,the code should be like this:

namespace me
{
const double PI = 3.14;
};

double a = me::pI * 28.

hope this could help.
 
J

James Kanze

You do not define a constant. You define a macro (which is not
bound to the namespace scope). The macro PI has the value "=
3.14;". Why don't you define a constant in the normal way:
const float Pi = 3.14;

Just a nit, but the "normal" way today is:

float const pi = 3.14 ;

(except that for a constant named pi, I'd expect something more
like 3.14159265358979323846:).

Some programmers (myself included) even like to add the keyword
static, to make it clear that the linkage is internal.
I don't think so. Can you give an example where it works
correctly? The perprocessor will change e.g.
MyFloat = PI;

MYFloat = = 3.14;;
which may be legal C++ code, but probably not what you want.

It's not legal C++. There's no way that the compiler can
convert "MyFloat = PI" into legal C++, given the definition,
because there will always be two successive = tokens, and there
is no production in the grammar which allows two successive =
tokens.
 
E

Erik Wikström

Just a nit, but the "normal" way today is:

float const pi = 3.14 ;

I suppose it depends on the environment you work in, but I would instead
have #included <math.h> (or <cmath>) and used M_PI. Of course, if you do
not have a POSIX compliant environment you might have to make your own.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top