By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

how to generate unique namespaces

P: n/a
er
hi,

here's why i'm trying to do:

header1.hpp
namespace{ struct A{};}
struct B1{ A a; };

header2.hpp
namespace{ struct A{};}
struct B2{ A a; };

*.cpp
#include <libs/testing_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>

header2.hpp|5|error: redefinition of ‘struct<unnamed>::A’|

i should expect that because the translation unit includes both
header1 and header 2. so then how can i automatically generate unique
namespaces, one for each of header1 and header2?

thanks.



Sep 18 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
er wrote:
hi,

here's why i'm trying to do:

header1.hpp
namespace{ struct A{};}
struct B1{ A a; };

header2.hpp
namespace{ struct A{};}
struct B2{ A a; };

*.cpp
#include <libs/testing_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>

header2.hpp|5|error: redefinition of ‘struct<unnamed>::A’|

i should expect that because the translation unit includes both
header1 and header 2. so then how can i automatically generate unique
namespaces, one for each of header1 and header2?
You can't. If you must, use a code generator.

If you automatically generate unique namespaces, how do you use them?

--
Ian Collins.
Sep 18 '08 #2

P: n/a
er
On Sep 17, 11:04*pm, Ian Collins <ian-n...@hotmail.comwrote:
er wrote:
hi,
here's why i'm trying to do:
header1.hpp
namespace{ struct A{};}
struct B1{ A a; };
header2.hpp
namespace{ struct A{};}
struct B2{ A a; };
*.cpp
#include <libs/testing_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>
header2.hpp|5|error: redefinition of ‘struct<unnamed>::A’|
i should expect that because the translation unit includes both
header1 and header 2. so then how can i automatically generate unique
namespaces, one for each of header1 and header2?

You can't. *If you must, use a code generator.

If you automatically generate unique namespaces, how do you use them?

--
Ian Collins.
thanks.

yes... you also have to use the unique_namespace::... throughout the
header, that's not practical.
Sep 18 '08 #3

P: n/a
On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.comwrote:
er wrote:
here's why i'm trying to do:
header1.hpp
namespace{ struct A{};}
struct B1{ A a; };
header2.hpp
namespace{ struct A{};}
struct B2{ A a; };
*.cpp
#include <libs/testing_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>
header2.hpp|5|error: redefinition of ?struct<unnamed>::A?|
i should expect that because the translation unit includes
both header1 and header 2. so then how can i automatically
generate unique namespaces, one for each of header1 and
header2?
You can't.
For some definition of "unique". Something like:

namespace header1_private { /* ... */ }

is sufficient for a lot of cases. Or for the really paranoid:
corporate_name_division_name_departement_name_head er1.

And since nobody has mentionned it: you almost never want an
anonymous namespace in a header. In his case, for example, if
header1.hpp is included in more than one translation unit, he
has undefined behavior, because of a violation of the one
definition rule for B1.
If you must, use a code generator.
Like you do for the include guards?
If you automatically generate unique namespaces, how do you
use them?
As you said, with a code generator:-). As long as everything is
generated by the same code generator, everything is hunky dory.
(This works for include guards because except for the #ifdef and
immediately following #define, you never use the symbol. And
those are automatically inserted by the "code generator" when
you create the file.)

--
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
Sep 18 '08 #4

P: n/a
How to merge a namespace with another namespace with different header
like C++ did as namespace std consists of many header file?

Thanks.
Sep 18 '08 #5

P: n/a
On Sep 18, 9:08 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
PeterAP...@gmail.com a écrit :
How to merge a namespace with another namespace with different header
like C++ did as namespace std consists of many header file?

Simply use the same namespace name:

a.h
-------

namespace myspace
{
struct A {};}

-----------------

b.h
-------

namespace myspace
{
struct B {};}

-----------------

myspace::A var_a;
myspace::B var_b;


A billion thanks for your help.
Sep 20 '08 #6

P: n/a
er
On Sep 18, 4:55*am, James Kanze <james.ka...@gmail.comwrote:
On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.comwrote:
er wrote:
here's why i'm trying to do:
header1.hpp
namespace{ struct A{};}
struct B1{ A a; };
header2.hpp
namespace{ struct A{};}
struct B2{ A a; };
*.cpp
#include <libs/testing_namespace/header1.hpp>
#include <libs/testing_namespace/header2.hpp>
header2.hpp|5|error: redefinition of ?struct<unnamed>::A?|
i should expect that because the translation unit includes
both header1 and header 2. so then how can i automatically
generate unique namespaces, one for each of header1 and
header2?
You can't.

For some definition of "unique". *Something like:

* * namespace header1_private { /* ... */ }
Yep, that is also what I have turned to. Thanks for this and the other
answers.
is sufficient for a lot of cases. *Or for the really paranoid:
corporate_name_division_name_departement_name_head er1.

And since nobody has mentionned it: you almost never want an
anonymous namespace in a header. *In his case, for example, if
header1.hpp is included in more than one translation unit, he
has undefined behavior, because of a violation of the one
definition rule for B1.
If you must, use a code generator.

Like you do for the include guards?
If you automatically generate unique namespaces, how do you
use them?

As you said, with a code generator:-). *As long as everything is
generated by the same code generator, everything is hunky dory.
(This works for include guards because except for the #ifdef and
immediately following #define, you never use the symbol. *And
those are automatically inserted by the "code generator" when
you create the file.)

--
James Kanze (GABI Software) * * * * * * email:james.ka...@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
Sep 20 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.