 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Louis Lavery Guest
|
Posted: Wed Nov 23, 2005 2:07 pm Post subject: How to import constants |
|
|
Hello,
What is a good way to import constants (which have common names
from one namespace to another?
As an example of what I mean, a library called gfx provides a
class, Colour, and some constants (which have common names).
A header file for use by users of gfx is...
/* gfx.hpp */
namespace gfx
{
class Colour {...};
Colour const red(1,0,0);
Colour const green(0,1,0);
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Wed Nov 23, 2005 8:41 pm Post subject: Re: How to import constants |
|
|
Louis Lavery wrote:
| Quote: |
...but that breaks the rule "never put a using directive in a header".
|
The rule I use is "never put a using directive *at global scope* in a
header". So you are not breaking my rule, you may consider relaxing
yours ;-)
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Sat Nov 26, 2005 9:43 pm Post subject: Re: How to import constants |
|
|
Louis Lavery wrote:
| Quote: | Hello,
What is a good way to import constants (which have common names
from one namespace to another?
|
The unasked question might be: what is a good reason for importing
constants (which have common names) from one namespace to another? In
other words, what are these identifiers doing in the first namespace if
they better suited for residence in the second namespace?
Since the names and purposes of namespaces are essentially arbitrary -
a program can declare as many namespaces and in whatever range and
depth that it wants. With all this flexibility, it should therefore be
possible for a program to find a single namespace for any set of
identifiers that all clients could reference. What would be the benefit
of duplication, of having one namespace import names from another?
Especially since the client has to include the names from the original
namespace anyway in order for them to be imported reduntantly into the
second?
It is possible to alias a namespace, so perhaps the goal in moving the
names is to simulate a namespace alias - an example of which is shown
below. In this program the user::colors namespace is actually just
another name for the gfx::colors namespace:
namespace gfx
{
namespace colors
{
const Color red(1,0,0);
const Color green(0, 1, 0);
...
}
}
namespace user
{
namespace colors = ::gfx::colors;
}
A namespace alias in a sense lets a client rename any namespace with a
different name that it likes better. I'm not sure that the interests of
code clarity are much advanced by using a namespace alias for this
purpose, but there is probably not much harm in it either.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|