Duplicate global constants

W

Will

Hi,

I'm linking into a library that has a global constant with the same
name as one in my application. My references to the constant are
being linked to the value in the library instead of the one in my
source file. Both constants are extern so I'm assuming it's undefined
which value I get. Obviously this is one of the reasons why global
constants are usually a bad idea.

I'm considering using a namespace to better encapsulate my constant
values. However, I'm working on a huge application with many
constants, which are used all over the place. So this would be a
significant change. Is this the best long term solution? Are there
other patterns for encapsulating constant values?

Thanks,
Will
 
G

Guest

Will said:
I'm considering using a namespace to better encapsulate my constant
values. However, I'm working on a huge application with many
constants, which are used all over the place. So this would be a

Using globals (especially menay of them) is considered a bad idea in C++
 
P

Phlip

Will said:
I'm linking into a library that has a global constant with the same
name as one in my application. My references to the constant are
being linked to the value in the library instead of the one in my
source file. Both constants are extern so I'm assuming it's undefined
which value I get. Obviously this is one of the reasons why global
constants are usually a bad idea.

I'm considering using a namespace to better encapsulate my constant
values. However, I'm working on a huge application with many
constants, which are used all over the place. So this would be a
significant change. Is this the best long term solution? Are there
other patterns for encapsulating constant values?

Firstly, code that only ever says "extern const int q = 5;" and never says
"const int q = 5;" is code that relies on an old sloppy C technique that
C++ no longer permits. 'q' must live somewhere - in a home translation unit
- to link, even though it's constant.

The solution is 'static const int q = 5;'. This only has scope in the
current translation unit, and lives there, one copy per module.

In general, your code should must use much better style (namespaces, no
globals, etc.) to stay ahead of this legacy code.
 
P

Pete Becker

Will said:
Hi,

I'm linking into a library that has a global constant with the same
name as one in my application. My references to the constant are
being linked to the value in the library instead of the one in my
source file. Both constants are extern so I'm assuming it's undefined
which value I get. Obviously this is one of the reasons why global
constants are usually a bad idea.

I'm considering using a namespace to better encapsulate my constant
values. However, I'm working on a huge application with many
constants, which are used all over the place. So this would be a
significant change. Is this the best long term solution? Are there
other patterns for encapsulating constant values?

For a short-term, minimal change, this should work:

namespace hide_this_name
{
const int i = 3;
}

using hide_this_name::i;

// pre-existing code that uses "i" goes here

Wherever your source code uses "i" the compiler will use the name
hide_this_name::i (in whatever form the compiler uses). That won't
conflict with the name "i" in the library, so long as you aren't using
any headers that refer to that version of "i".
 
F

Fei Liu

Will said:
Hi,

I'm linking into a library that has a global constant with the same
name as one in my application. My references to the constant are
being linked to the value in the library instead of the one in my
source file. Both constants are extern so I'm assuming it's undefined
which value I get. Obviously this is one of the reasons why global
constants are usually a bad idea.

I'm considering using a namespace to better encapsulate my constant
values. However, I'm working on a huge application with many
constants, which are used all over the place. So this would be a
significant change. Is this the best long term solution? Are there
other patterns for encapsulating constant values?

Thanks,
Will

Use namespace, one of its design purpose is to avoid name space
pollution, as what's going on in your case.
 
T

Tomás

Will posted:
Hi,

I'm linking into a library that has a global constant with the same
name as one in my application. My references to the constant are
being linked to the value in the library instead of the one in my
source file. Both constants are extern so I'm assuming it's undefined
which value I get. Obviously this is one of the reasons why global
constants are usually a bad idea.

I'm considering using a namespace to better encapsulate my constant
values. However, I'm working on a huge application with many
constants, which are used all over the place. So this would be a
significant change. Is this the best long term solution? Are there
other patterns for encapsulating constant values?

Thanks,
Will


Even if you don't find a solution you like, God created "Find and
Replace" for a reason : ).


-Tomás
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top