question about anonymous namespace...

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

    question about anonymous namespace...

    I am reading the book "C++ Annotations", and here is a quote from the
    book:

    Namespaces can be defined without a name. Such a namespace is
    anonymous and it restricts the
    visibility of the defined entities to the source file in which the
    anonymous namespace is defined.
    Entities defined in the anonymous namespace are comparable to C’s
    static functions and variables.
    In C++ the static keyword can still be used, but its use is more
    common in class definitions
    (see chapter 6). In situations where static variables or functions are
    necessary, the use of the
    anonymous namespace is preferred.


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

    Could anybody give me an example about why the anonymous name space is
    the same as "static" variables and functions in C?

    thanks!
  • Ian Collins

    #2
    Re: question about anonymous namespace...

    Luna Moon wrote:
    I am reading the book "C++ Annotations", and here is a quote from the
    book:
    >
    Namespaces can be defined without a name. Such a namespace is
    anonymous and it restricts the
    visibility of the defined entities to the source file in which the
    anonymous namespace is defined.
    Entities defined in the anonymous namespace are comparable to C’s
    static functions and variables.
    In C++ the static keyword can still be used, but its use is more
    common in class definitions
    (see chapter 6). In situations where static variables or functions are
    necessary, the use of the
    anonymous namespace is preferred.
    >
    >
    --------------
    >
    Could anybody give me an example about why the anonymous name space is
    the same as "static" variables and functions in C?
    >
    The quote says it all: "it restricts the visibility of the defined
    entities to the source file in which the anonymous namespace is defined"

    Which is exactly what "static" does in C.

    --
    Ian Collins

    Comment

    • blargg

      #3
      Re: question about anonymous namespace...

      In article
      <4b97d33c-17f1-4bf8-a02b-71ca05250278@a2 9g2000pra.googl egroups.com>, Luna
      Moon <lunamoonmoon@g mail.comwrote:
      I am reading the book "C++ Annotations", and here is a quote from the
      book:
      >
      Namespaces can be defined without a name. Such a namespace is
      anonymous and it restricts the visibility of the defined entities
      to the source file in which the anonymous namespace is defined.
      Entities defined in the anonymous namespace are comparable to C's
      static functions and variables. In C++ the static keyword can still
      be used, but its use is more common in class definitions (see
      chapter 6). In situations where static variables or functions are
      necessary, the use of the anonymous namespace is preferred.
      >
      --------------
      >
      Could anybody give me an example about why the anonymous name space is
      the same as "static" variables and functions in C?
      Why? Because the standard says so. Actually, the effect is not exactly the same.

      // Before namespace
      static int i;
      static void f() { }
      struct my_foo { };

      // After namespace
      namespace
      {
      int i;
      void f() { }
      struct foo { }; // no need for prefix on name, since it's now a local name
      }

      BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1). The author of
      the C++ book you're reading should have taken the time to get it right.

      Comment

      • James Kanze

        #4
        Re: question about anonymous namespace...

        On Oct 23, 9:08 am, blargg....@gish puppy.com (blargg) wrote:
        In article
        <4b97d33c-17f1-4bf8-a02b-71ca05250...@a2 9g2000pra.googl egroups.com>,
        Luna Moon <lunamoonm...@g mail.comwrote:
        [...]
        Could anybody give me an example about why the anonymous
        name space is the same as "static" variables and functions
        in C?
        Why? Because the standard says so. Actually, the effect is not
        exactly the same.
        // Before namespace
        static int i;
        static void f() { }
        struct my_foo { };
        // After namespace
        namespace
        {
        int i;
        void f() { }
        struct foo { }; // no need for prefix on name, since it's now a localname
        }
        Another important difference is the linkage of the name. A
        static variable or function has internal linkage. A name in an
        anonymous namespace has external linkage---the reason it can't
        be seen from other translation units is that it isn't namable in
        them. (Formally, it is visible everywhere, but since it can't
        be named, there's no way to actually see it.) A template can
        only be instantiated over something that has external linkage,
        e.g.:

        template< int* p class T {} ;

        static int i ;
        namespace {
        int j ;
        }

        T< &i ti ; // illegal...
        T< &j tj ; // legal.
        BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1).
        The author of the C++ book you're reading should have taken
        the time to get it right.
        The term anonymous namespace seems pretty widespread to me.

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

        • spambouncer@gmx.de

          #5
          Re: question about anonymous namespace...

          ... A template can only be instantiated over something that
          has external linkage, e.g.:
          >
          template< int* p class T {} ;
          >
          static int i ;
          namespace {
          int j ;
          }
          >
          T< &i ti ; // illegal...
          T< &j tj ; // legal.
          To me, anonymous namespaces were just the C++ way of saying static.
          The external linkage point is new to me. But:

          Pointer-to-int as template argument? Instantiating templates over
          something with external linkage? Could you provide a practical example
          working with anonymous namespace but not working with static? Note
          that you could also omit the static keyword to the same effect.
          BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1).
          The author of the C++ book you're reading should have taken
          the time to get it right.
          >
          The term anonymous namespace seems pretty widespread to me.
          I'd bet a horse and a carrot that "unnamed" is the literal meaning of
          "anonymous" .

          Best regards,
          Andreas.

          Comment

          • Michael  Mol

            #6
            Re: question about anonymous namespace...

            On Oct 23, 3:08 am, blargg....@gish puppy.com (blargg) wrote:

            [snip]
            // Before namespace
            static int i;
            static void f() { }
            struct my_foo { };
            >
            // After namespace
            namespace
            {
                int i;
                void f() { }
                struct foo { }; // no need for prefix on name, since it's now a local name
            >
            }

            Now I'm curious. Take the following code from the same compilation
            unit:

            namespace
            {
            int i;
            void x() { }
            }

            namespace
            {
            int j;
            void y() { }
            }

            Would i be visible to y(), and j be visible to x()?

            Comment

            • Pete Becker

              #7
              Re: question about anonymous namespace...

              On 2008-10-23 12:23:54 -0400, Michael Mol <mikemol@gmail. comsaid:
              On Oct 23, 3:08 am, blargg....@gish puppy.com (blargg) wrote:
              >
              [snip]
              >
              >// Before namespace
              >static int i;
              >static void f() { }
              >struct my_foo { };
              >>
              >// After namespace
              >namespace
              >{
              >    int i;
              >    void f() { }
              >    struct foo { }; // no need for prefix on name, since it's now a l
              ocal name
              >>
              >}
              >
              >
              Now I'm curious. Take the following code from the same compilation
              unit:
              >
              namespace
              {
              int i;
              void x() { }
              }
              >
              namespace
              {
              int j;
              void y() { }
              }
              >
              Would i be visible to y(),
              yes.
              and j be visible to x()?
              no.

              Same answer with a named namespace and without a namespace.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              • spambouncer@gmx.de

                #8
                Re: question about anonymous namespace...

                Now I'm curious. Take the following code from the same compilation
                unit:
                >
                namespace
                {
                int i;
                void x() { }
                }
                >
                namespace
                {
                int j;
                void y() { }
                }
                >
                Would i be visible to y(), and j be visible to x()?
                Generally, namespaces can be split across several compilation units.
                The only exception to this rule are anonymous namespaces. If you would
                split an anonymous namespace across several compilation units, it
                wouldn't be the same namespace. That's because anonymous namespaces
                are not truely anonymous, it's just that their name is not accessible
                by the coder. The compiler, however, assigns a unique ID to them per
                compilation unit.

                But, as you're asking for the same compilation unit, yes, i is visible
                to y(), and no, j is not visible to x().

                Best regards,
                Andreas.

                Comment

                • blargg

                  #9
                  Re: question about anonymous namespace...

                  In article
                  <340f72bf-6bd6-4d58-8e3a-69d9805e7c71@g6 1g2000hsf.googl egroups.com>,
                  Michael Mol <mikemol@gmail. comwrote:
                  On Oct 23, 3:08 am, blargg....@gish puppy.com (blargg) wrote:
                  >
                  [snip]
                  >
                  // Before namespace
                  static int i;
                  static void f() { }
                  struct my_foo { };

                  // After namespace
                  namespace
                  {
                  int i;
                  void f() { }
                  struct foo { }; // no need for prefix on name, since it's now a
                  // local name

                  }
                  >
                  Now I'm curious. Take the following code from the same compilation
                  unit:
                  >
                  namespace
                  {
                  int i;
                  void x() { }
                  }
                  >
                  namespace
                  {
                  int j;
                  void y() { }
                  }
                  >
                  Would i be visible to y(), and j be visible to x()?
                  Heck, since we're talking about the same translation unit, you could also add

                  // at file scope in same translation unit
                  void z()
                  {
                  i = 0;
                  j = 0;
                  x();
                  y();
                  }

                  and it would see everything in your unnamed namespaces (and z would be
                  visible to other translation units).

                  Comment

                  • James Kanze

                    #10
                    Re: question about anonymous namespace...

                    On Oct 23, 5:35 pm, "spamboun...@gm x.de" <spamboun...@gm x.dewrote:
                    ... A template can only be instantiated over something that
                    has external linkage, e.g.:
                        template< int* p class T {} ;
                        static int i ;
                        namespace {
                            int j ;
                        }
                        T< &i ti ;   //  illegal...
                        T< &j tj ;   //  legal.
                    To me, anonymous namespaces were just the C++ way of saying
                    static. The external linkage point is new to me. But:
                    Pointer-to-int as template argument?
                    Why not? It was the simplest example that came to my mind.
                    Instantiating templates over something with external linkage?
                    Anything you use to instantiate a template must have external
                    linkage. Those are the rules.
                    Could you provide a practical example working with anonymous
                    namespace but not working with static?
                    I just did. See above: declare the variable static, and you
                    can't use it to instantiate the template. Define it in an
                    anonymous namespace, and you can.

                    I run into this a lot because of const; const is static by
                    default, so something like:

                    template< int* p class T{} ;

                    namespace {
                    int const i = 43 ;
                    }

                    T< &i t ; // illegal.

                    You have to define the int:
                    extern int const i = 43 ;

                    (In practice, of course, most of the time, it's a user defined
                    class, and the template parameter is a reference, rather than a
                    pointer, and it's a function template, not a class template.
                    But the same principles apply.)
                    Note that you could also omit the static keyword to the same
                    effect.
                    Except that if I omitted the static keyword in my example, the
                    symbol would be visible in every module. And conflict with
                    another module which did exactly the same thing with the same
                    name.
                    BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1).
                    The author of the C++ book you're reading should have taken
                    the time to get it right.
                    The term anonymous namespace seems pretty widespread to me.
                    I'd bet a horse and a carrot that "unnamed" is the literal
                    meaning of "anonymous" .
                    Actually, no. From the way I understand English, anonymous
                    means that the name is hidden, and not visible; unnamed means
                    that it doesn't exist. In which case, anonymous is actually
                    closer to what really happens.

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

                    • Hendrik Schober

                      #11
                      Re: question about anonymous namespace...

                      James Kanze wrote:
                      On Oct 23, 5:35 pm, "spamboun...@gm x.de" <spamboun...@gm x.dewrote:
                      [...]
                      >I'd bet a horse and a carrot that "unnamed" is the literal
                      >meaning of "anonymous" .
                      >
                      Actually, no. From the way I understand English, anonymous
                      means that the name is hidden, and not visible; unnamed means
                      that it doesn't exist. In which case, anonymous is actually
                      closer to what really happens.
                      According to my dictionary, "anonymous" derives from an old
                      Latin word which derives from a Greek word, consisting of "an"
                      ("without") and "onuma" ("name"), and meaning "nameless".
                      I know, of course, that this leaves a lot of room for a debate
                      about whether today this actually is "the literal meaning" of
                      the word or not... However, my dictionary then produces "having
                      an unknown or unacknowledged name" as the first of the three
                      meanings it comes up with. (And now there's room for a debate
                      whether my dictionary is actually worth anything...)

                      Schobi

                      Comment

                      • Luna Moon

                        #12
                        Re: question about anonymous namespace...

                        On Oct 23, 9:27 am, Pete Becker <p...@versatile coding.comwrote :
                        On 2008-10-23 12:23:54 -0400, Michael  Mol <mike...@gmail. comsaid:
                        >
                        >
                        >
                        >
                        >
                        On Oct 23, 3:08 am, blargg....@gish puppy.com (blargg) wrote:
                        >
                        [snip]
                        >
                        // Before namespace
                        static int i;
                        static void f() { }
                        struct my_foo { };
                        >
                        // After namespace
                        namespace
                        {
                            int i;
                            void f() { }
                            struct foo { }; // no need for prefix on name, since it's now a l
                        ocal name
                        >
                        }
                        >
                        Now I'm curious.  Take the following code from the same compilation
                        unit:
                        >
                        namespace
                        {
                            int i;
                            void x() { }
                        }
                        >
                        namespace
                        {
                            int j;
                            void y() { }
                        }
                        >
                        Would i be visible to y(),
                        >
                        yes.
                        >
                         and j be visible to x()?
                        >
                        no.
                        >
                        Same answer with a named namespace and without a namespace.
                        >
                        Why the above answers Pete? Thanks!

                        Comment

                        • Luna Moon

                          #13
                          Re: question about anonymous namespace...

                          On Oct 22, 4:05 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                          Luna Moon wrote:
                          I am reading the book "C++ Annotations", and here is a quote from the
                          book:
                          >
                          Namespaces can be defined without a name. Such a namespace is
                          anonymous and it restricts the
                          visibility of the defined entities to the source file in which the
                          anonymous namespace is defined.
                          Entities defined in the anonymous namespace are comparable to C’s
                          static functions and variables.
                          In C++ the static keyword can still be used, but its use is more
                          common in class definitions
                          (see chapter 6). In situations where static variables or functions are
                          necessary, the use of the
                          anonymous namespace is preferred.
                          >
                          --------------
                          >
                          Could anybody give me an example about why the anonymous name space is
                          the same as "static" variables and functions in C?
                          >
                          The quote says it all: "it restricts the visibility of the defined
                          entities to the source file in which the anonymous namespace is defined"
                          >
                          Which is exactly what "static" does in C.
                          >
                          --
                          Ian Collins- Hide quoted text -
                          >
                          - Show quoted text -
                          I thought "static" means "persistent ", are the anonymous name space
                          persistent?

                          If you define "static" in a function, it will only get initialized
                          once and can be used forever...

                          Comment

                          • Ian Collins

                            #14
                            Re: question about anonymous namespace...

                            Luna Moon wrote:
                            On Oct 22, 4:05 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                            >Luna Moon wrote:
                            >>I am reading the book "C++ Annotations", and here is a quote from the
                            >>book:
                            >>Namespaces can be defined without a name. Such a namespace is
                            >>anonymous and it restricts the
                            >>visibility of the defined entities to the source file in which the
                            >>anonymous namespace is defined.
                            >>Entities defined in the anonymous namespace are comparable to C’s
                            >>static functions and variables.
                            >>In C++ the static keyword can still be used, but its use is more
                            >>common in class definitions
                            >>(see chapter 6). In situations where static variables or functions are
                            >>necessary, the use of the
                            >>anonymous namespace is preferred.
                            >>--------------
                            >>Could anybody give me an example about why the anonymous name space is
                            >>the same as "static" variables and functions in C?
                            >The quote says it all: "it restricts the visibility of the defined
                            >entities to the source file in which the anonymous namespace is defined"
                            >>
                            >Which is exactly what "static" does in C.
                            >>
                            Last signature warning!
                            >
                            I thought "static" means "persistent ", are the anonymous name space
                            persistent?
                            >
                            If you define "static" in a function, it will only get initialized
                            once and can be used forever...
                            >
                            Static in the context of C and linkage. It's unfortunate that both C
                            and C++ overload the meaning of static. Attempting to remove this
                            ambiguity is probably one of the reasons C++ introduced the unnamed
                            namespace.

                            --
                            Ian Collins

                            Comment

                            • Pete Becker

                              #15
                              Re: question about anonymous namespace...

                              On 2008-10-23 17:19:58 -0400, Luna Moon <lunamoonmoon@g mail.comsaid:
                              On Oct 23, 9:27 am, Pete Becker <p...@versatile coding.comwrote :
                              >On 2008-10-23 12:23:54 -0400, Michael  Mol <mike...@gmail. comsaid:
                              >>
                              >>
                              >>
                              >>
                              >>
                              >>On Oct 23, 3:08 am, blargg....@gish puppy.com (blargg) wrote:
                              >>
                              >>[snip]
                              >>
                              >>>// Before namespace
                              >>>static int i;
                              >>>static void f() { }
                              >>>struct my_foo { };
                              >>
                              >>>// After namespace
                              >>>namespace
                              >>>{
                              >>>    int i;
                              >>>    void f() { }
                              >>>    struct foo { }; // no need for prefix on name, since it's now
                              a l
                              >>ocal name
                              >>
                              >>>}
                              >>
                              >>Now I'm curious.  Take the following code from the same compilation
                              >>unit:
                              >>
                              >>namespace
                              >>{
                              >>    int i;
                              >>    void x() { }
                              >>}
                              >>
                              >>namespace
                              >>{
                              >>    int j;
                              >>    void y() { }
                              >>}
                              >>
                              >>Would i be visible to y(),
                              >>
                              >yes.
                              >>
                              >> and j be visible to x()?
                              >>
                              >no.
                              >>
                              >Same answer with a named namespace and without a namespace.
                              >>
                              >
                              Why the above answers Pete? Thanks!
                              The above answers were intended to inspire thinking and experimenting.

                              --
                              Pete
                              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                              Standard C++ Library Extensions: a Tutorial and Reference
                              (www.petebecker.com/tr1book)

                              Comment

                              Working...