how to generate unique namespaces

E

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.
 
I

Ian Collins

er said:
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?
 
E

er

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

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

thanks.

yes... you also have to use the unique_namespace::... throughout the
header, that's not practical.
 
A

Alexander Dong Back Kim

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

I think Ian is quite right. I'm not sure what exactly you're trying to
do here but..

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

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

cheers,
Alex Kim
 
J

James Kanze

er wrote:
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_header1.

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.)
 
P

PeterAPIIT

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

Thanks.
 
M

Michael DOUBEZ

(e-mail address removed) 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 {};
}
 
P

PeterAPIIT

(e-mail address removed) a écrit :


Simply use the same namespace name:

a.h
-------

namespace myspace
{
struct A {};}

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

b.h
-------

namespace myspace
{
struct B {};}



A billion thanks for your help.
 
E

er

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.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top