Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static const variables within a namespace

Reply
Thread Tools

Static const variables within a namespace

 
 
Sehcra
Guest
Posts: n/a
 
      07-21-2005
Hi,

I'm trying to figure out if what I'm doing makes any sense. I created
a namespace that contains some functions as well as some constants.
Because these variables are constant, I have no need for there to be
more than one copy of them. Does it make sense to make these
variables static const, or do I just make them const? If this were a
class instead of a namespace, I would obviously want to make the
variables static, but this kind of reasoning doesn't seem to apply to
namespaces.

Thanks.
 
Reply With Quote
 
 
 
 
Wang, Dong
Guest
Posts: n/a
 
      07-21-2005
Sehcra wrote:
> Hi,
>
> I'm trying to figure out if what I'm doing makes any sense. I created
> a namespace that contains some functions as well as some constants.
> Because these variables are constant, I have no need for there to be
> more than one copy of them. Does it make sense to make these
> variables static const, or do I just make them const? If this were a
> class instead of a namespace, I would obviously want to make the
> variables static, but this kind of reasoning doesn't seem to apply to
> namespaces.
>
> Thanks.

I think basically it doesn't make much sense to qualify an extra static
on to a constant, even in a class. A constant is just a constant, which
has nothing to do with static or "dynamic" (if it ever existed).
Actually, from some sort of perspective, you can think of a constant as
a static.

The use of "static" is generally not recommended unless inside functions
(statically allocated) or classes (part of the classes, rather than each
object).

Thanks.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-21-2005
* Sehcra:
>
> I'm trying to figure out if what I'm doing makes any sense. I created
> a namespace that contains some functions as well as some constants.
> Because these variables are constant, I have no need for there to be
> more than one copy of them. Does it make sense to make these
> variables static const, or do I just make them const?


At namespace scope a constant already has internal linkage by default, so
adding 'static' is only adding a deprecated feature that does nothing.

And if these definitions are in a header file, the result is any way the
opposite of the stated goal (to have only one copy of each).

You could add 'extern' to make the linkage external, if what you want is a
single copy of each constant in the whole program. Then you have only the
declarations in your header file, and definitions in an implementation file.
Or to avoid an implementation file you could define inline functions.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      07-22-2005

Sehcra wrote:
> Hi,
>
> I'm trying to figure out if what I'm doing makes any sense. I created
> a namespace that contains some functions as well as some constants.
> Because these variables are constant, I have no need for there to be
> more than one copy of them. Does it make sense to make these
> variables static const, or do I just make them const? If this were a
> class instead of a namespace, I would obviously want to make the
> variables static, but this kind of reasoning doesn't seem to apply to
> namespaces.
>
> Thanks.


I wouldn't worry too much about declared constants. Unless the program
takes the address, or passes by reference, the constant, it's unlikely
that the compiler is even allocating even one instance of the constant,
let alone more than one.

Think of a const variable declaration with a constant expression as its
initializer, such as:

const int kOneDozen = 12;

as a more modern, type-safe replacement for the C practice of using
#define to create a constant value.

Greg

 
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
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
difference between static const and const at the namespace level cppsks C++ 2 02-10-2005 06:20 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments