473,491 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Static const variables within a namespace

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.
Jul 23 '05 #1
3 7296
* 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?
Jul 23 '05 #2
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.
Jul 23 '05 #3

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

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2817
by: Michael Klatt | last post by:
Is there any practical difference between a local variable in main() declared 'const' and one declared 'static const'? int main() { static const int i1(0); const int i2(0); return 0; }
4
5401
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside...
2
3569
by: Drew McCormack | last post by:
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...
2
7480
by: Daniel Switkin | last post by:
Hi there, I'm trying to do the following: class tLimits { static const int kIntMin = 0; // fine static const float kFloatMin = 0.0f; // breaks }; and I get this message:
12
2869
by: Philip Potter | last post by:
I'm reading the comp.lang.c++ faq and, though it is incredibly clear and lucid in many points, I am completely confused by question 29.6: ...
5
3580
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
1
9065
by: david.baird | last post by:
I have 3 classes, 1 which does not use templates, CGood, and 2 which do use templates, CBad, and COkay. I believe that all of them should work, but only CGood and COkay are working for me. CBad...
2
1601
by: WinniePooh | last post by:
Hi there, I've the following code (here a snippet). ///////////////////////////////// // *.h file ///////////////////////////////// class ParameterTypeNames { public: static const...
6
1367
by: brekehan | last post by:
I am embaressed to ask such a newbie question, but hey, we all forget things. If I want to define some constants within a namespace and share those constants with multiple cpp files. What is the...
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.