how to generate unique namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • er

    how to generate unique namespaces

    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_namespa ce/header1.hpp>
    #include <libs/testing_namespa ce/header2.hpp>

    header2.hpp|5|e rror: 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.







  • Ian Collins

    #2
    Re: how to generate unique namespaces

    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_namespa ce/header1.hpp>
    #include <libs/testing_namespa ce/header2.hpp>
    >
    header2.hpp|5|e rror: 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.

    Comment

    • er

      #3
      Re: how to generate unique namespaces

      On Sep 17, 11:04 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      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_namespa ce/header1.hpp>
      #include <libs/testing_namespa ce/header2.hpp>
      >
      header2.hpp|5|e rror: 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_namespac e::... throughout the
      header, that's not practical.


      Comment

      • James Kanze

        #4
        Re: how to generate unique namespaces

        On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.co mwrote:
        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_namespa ce/header1.hpp>
        #include <libs/testing_namespa ce/header2.hpp>
        header2.hpp|5|e rror: 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_d epartement_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:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • PeterAPIIT@gmail.com

          #5
          Re: how to generate unique namespaces

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

          Thanks.

          Comment

          • PeterAPIIT@gmail.com

            #6
            Re: how to generate unique namespaces

            On Sep 18, 9:08 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
            PeterAP...@gmai l.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.

            Comment

            • er

              #7
              Re: how to generate unique namespaces

              On Sep 18, 4:55 am, James Kanze <james.ka...@gm ail.comwrote:
              On Sep 18, 5:04 am, Ian Collins <ian-n...@hotmail.co mwrote:
              >
              >
              >
              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_namespa ce/header1.hpp>
              #include <libs/testing_namespa ce/header2.hpp>
              header2.hpp|5|e rror: 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_d epartement_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:james.ka. ..@gmail.com
              Conseils en informatique orientée objet/
                                 Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              Working...