473,785 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Hell o");

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

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

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 1615
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("Hell o");

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

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

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...@schwabcen ter.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("Hell o");
b.cpp
--------
#include <string>
const int TheNumber = 1;
const std::string TheString("Hell o");
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...@schwabcen ter.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("Hell o");
b.cpp
--------
#include <string>
const int TheNumber = 1;
const std::string TheString("Hell o");
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 objektorientier ter Datenverarbeitu ng
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...@schwabcen ter.comwrote:
>newbar...@gmai l.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("Hell o");
>>b.cpp
--------
#include <string>
>>const int TheNumber = 1;
const std::string TheString("Hell o");
>>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
2512
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 keyword a lot less usable. Can anybody tell me what that good reason is? TIA,
6
2424
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 implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an uncommitted pointer class because same copies will be committed as two separate objects on disk....
5
22061
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 example). int foo(const char* argv) { return 0; } int main(int argc, char* argv) {
66
3520
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 values side. IIRC, "apply" used to require that the second argument be a tuple; it now accepts sequences, and has been depreciated in favor of *args, which accepts not only sequences but iterators. Is there any place in the language that still...
9
1410
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 with threads and am looking for some pointers on how to implement, and if the lower level modules/objects need to be rewritten to use threading.local for all local variables. I have a module that communicates with a hardware device, which reads...
4
2188
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
4578
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
1876
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: main() { const int p =5; int* pp = &p; ++(*pp);
10
5975
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 is unused, I know that it does not take up storage in the
0
9484
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.