473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

const objects at module level

Hello,

Very basic question here. If I have the following constants declared
in a.cpp and in b.cpp, would there be any possibility of duplicate
symbols?:

a.cpp
--------
#include <string>

const int TheNumber = 1;
const std::string TheString("Hello");

b.cpp
--------
#include <string>

const int TheNumber = 1;
const std::string TheString("Hello");

Are they not inserted into the symbol table? Previously, I've put
these into an unnamed namespace to be sure but I'm code reviewing
others' code and they're questioning the need for an anonymous
namespace, and I suspect they're correct based on my small test
programs with VC++.

Thanks,

Pete
Mar 13 '08 #1
4 1589
ne*******@gmail.com wrote:
Hello,

Very basic question here. If I have the following constants declared
in a.cpp and in b.cpp, would there be any possibility of duplicate
symbols?:

a.cpp
--------
#include <string>

const int TheNumber = 1;
const std::string TheString("Hello");

b.cpp
--------
#include <string>

const int TheNumber = 1;
const std::string TheString("Hello");

Are they not inserted into the symbol table? Previously, I've put
these into an unnamed namespace to be sure but I'm code reviewing
others' code and they're questioning the need for an anonymous
namespace, and I suspect they're correct based on my small test
programs with VC++.
C++ constants at global scope have internal linkage by default. This is
different from C, where they have external linkage. The first reason
that comes to mind for this difference is so that C++ constants (with
proper static types) can effectively replace C-style preprocessor
constants (#define MAX 42).

Functions and non-const variables declared at global scope have default
external linkage in both C and C++.

FWIW, this surprised me, too. I group these constants into unnamed
namespaces as well, and probably will continue to do so, for reasons
vaguely akin to why I always put curly braces around loop bodies, even
when technically unnecessary.
Mar 13 '08 #2
On Mar 13, 3:12*pm, Jeff Schwab <j...@schwabcenter.comwrote:
newbar...@gmail.com wrote:
Hello,
Very basic question here. If I have the following constants declared
in a.cpp and in b.cpp, would there be any possibility of duplicate
symbols?:
a.cpp
--------
#include <string>
const int TheNumber = 1;
const std::string TheString("Hello");
b.cpp
--------
#include <string>
const int TheNumber = 1;
const std::string TheString("Hello");
Are they not inserted into the symbol table? Previously, I've put
these into an unnamed namespace to be sure but I'm code reviewing
others' code and they're questioning the need for an anonymous
namespace, and I suspect they're correct based on my small test
programs with VC++.

C++ constants at global scope have internal linkage by default. *This is
different from C, where they have external linkage. *The first reason
that comes to mind for this difference is so that C++ constants (with
proper static types) can effectively replace C-style preprocessor
constants (#define MAX 42).

Functions and non-const variables declared at global scope have default
external linkage in both C and C++.

FWIW, this surprised me, too. *I group these constants into unnamed
namespaces as well, and probably will continue to do so, for reasons
vaguely akin to why I always put curly braces around loop bodies, even
when technically unnecessary.- Hide quoted text -

- Show quoted text -
Jeff,

Thanks for the reply. I've also found Stroustrup say the same it's ok
in CPPPL3E section 9.2. (but he doesn't point out the difference with
C).

Pete
Mar 13 '08 #3
On Mar 13, 4:12 pm, Jeff Schwab <j...@schwabcenter.comwrote:
newbar...@gmail.com wrote:
Very basic question here. If I have the following constants
declared in a.cpp and in b.cpp, would there be any
possibility of duplicate symbols?:
a.cpp
--------
#include <string>
const int TheNumber = 1;
const std::string TheString("Hello");
b.cpp
--------
#include <string>
const int TheNumber = 1;
const std::string TheString("Hello");
Are they not inserted into the symbol table? Previously,
I've put these into an unnamed namespace to be sure but I'm
code reviewing others' code and they're questioning the need
for an anonymous namespace, and I suspect they're correct
based on my small test programs with VC++.
C++ constants at global scope have internal linkage by
default. This is different from C, where they have external
linkage. The first reason that comes to mind for this
difference is so that C++ constants (with proper static types)
can effectively replace C-style preprocessor constants
(#define MAX 42).
Which really isn't much of a reason, since you could aways write
"static const int ...", if that's what you wanted.
Functions and non-const variables declared at global scope
have default external linkage in both C and C++.
FWIW, this surprised me, too. I group these constants into
unnamed namespaces as well, and probably will continue to do
so, for reasons vaguely akin to why I always put curly braces
around loop bodies, even when technically unnecessary.
The case where it's really surprising is with regards to
templates. Something like "template< int const* PI ..." can't
be instantiated with the address of TheNumber, above. You have
to define it:
extern int const TheNumber = 1 ;
(Put it in an anonymous namespace, and you've effectively used
"extern" to define a local variable.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 14 '08 #4
James Kanze wrote:
On Mar 13, 4:12 pm, Jeff Schwab <j...@schwabcenter.comwrote:
>newbar...@gmail.com wrote:
>>Very basic question here. If I have the following constants
declared in a.cpp and in b.cpp, would there be any
possibility of duplicate symbols?:
>>a.cpp
--------
#include <string>
>>const int TheNumber = 1;
const std::string TheString("Hello");
>>b.cpp
--------
#include <string>
>>const int TheNumber = 1;
const std::string TheString("Hello");
>>Are they not inserted into the symbol table? Previously,
I've put these into an unnamed namespace to be sure but I'm
code reviewing others' code and they're questioning the need
for an anonymous namespace, and I suspect they're correct
based on my small test programs with VC++.
>C++ constants at global scope have internal linkage by
default. This is different from C, where they have external
linkage. The first reason that comes to mind for this
difference is so that C++ constants (with proper static types)
can effectively replace C-style preprocessor constants
(#define MAX 42).

Which really isn't much of a reason, since you could aways write
"static const int ...", if that's what you wanted.
True. Or use unnamed namespaces.
>Functions and non-const variables declared at global scope
have default external linkage in both C and C++.
>FWIW, this surprised me, too. I group these constants into
unnamed namespaces as well, and probably will continue to do
so, for reasons vaguely akin to why I always put curly braces
around loop bodies, even when technically unnecessary.

The case where it's really surprising is with regards to
templates. Something like "template< int const* PI ..." can't
be instantiated with the address of TheNumber, above. You have
to define it:
extern int const TheNumber = 1 ;
(Put it in an anonymous namespace, and you've effectively used
"extern" to define a local variable.)
That does seem odd. Do you know of a better reason for the default
internal linkage of global-scope constants? I do not.
Mar 14 '08 #5

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

Similar topics

20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
4
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.