how to generate unique namespaces

Discussion in 'C++' started by er, Sep 18, 2008.

  1. er

    er Guest

    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.
     
    er, Sep 18, 2008
    #1
    1. Advertising

  2. er

    Ian Collins Guest

    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.
     
    Ian Collins, Sep 18, 2008
    #2
    1. Advertising

  3. er

    er Guest

    On Sep 17, 11:04 pm, Ian Collins <> wrote:
    > 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.
     
    er, Sep 18, 2008
    #3
  4. On Sep 18, 1:04 pm, Ian Collins <> wrote:
    > 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>

    >
    > If you automatically generate unique namespaces, how do you use them?
    >
    > --
    > Ian Collins.


    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
     
    Alexander Dong Back Kim, Sep 18, 2008
    #4
  5. er

    James Kanze Guest

    On Sep 18, 5:04 am, Ian Collins <> wrote:
    > 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_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.)

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Sep 18, 2008
    #5
  6. er

    Guest

    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, 2008
    #6
  7. 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;


    --
    Michael
     
    Michael DOUBEZ, Sep 18, 2008
    #7
  8. er

    Guest

    On Sep 18, 9:08 pm, Michael DOUBEZ <> wrote:
    > 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, 2008
    #8
  9. er

    er Guest

    On Sep 18, 4:55 am, James Kanze <> wrote:
    > On Sep 18, 5:04 am, Ian Collins <> wrote:
    >
    >
    >
    > > 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_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.)
    >
    > --
    > James Kanze (GABI Software)             email:
    > 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
     
    er, Sep 20, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald
    Replies:
    6
    Views:
    7,077
    Andy Mortimer [MS]
    Feb 23, 2004
  2. Max
    Replies:
    5
    Views:
    32,820
    Sudsy
    Feb 28, 2004
  3. Dizzy Haze

    Namespaces and unique IDs

    Dizzy Haze, Sep 27, 2005, in forum: XML
    Replies:
    6
    Views:
    560
    Henry S. Thompson
    Sep 30, 2005
  4. ToshiBoy
    Replies:
    6
    Views:
    885
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    391
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page