Namespace inclusion

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

    Namespace inclusion

    Hi,

    What is the difference between using "using std::X" instead of writing
    "std::XInstance " every time I use the X instance? Is the latter one
    more efficient in the context of compiler dependencies?

    Thanks.
  • Michael DOUBEZ

    #2
    Re: Namespace inclusion

    D. Susman a écrit :
    Hi,
    >
    What is the difference between using "using std::X" instead of writing
    "std::XInstance " every time I use the X instance? Is the latter one
    more efficient in the context of compiler dependencies?
    no. but it is more efficient in terms of readability and typing.
    This doesn't show so much with namespace std but when working with
    boost, it is a necessity (especially with boost::date_tim e :) ).

    Michael

    Comment

    • Jeff Schwab

      #3
      Re: Namespace inclusion

      D. Susman wrote:
      Hi,
      >
      What is the difference between using "using std::X" instead of writing
      "std::XInstance " every time I use the X instance? Is the latter one
      more efficient in the context of compiler dependencies?
      The former tells the compiler: "When you're looking for something
      called X, please consider the thing called std::X."

      The latter tells the compilre: "I specifically want to use std::X here."

      The former is generally preferable for functions, because it allows
      argument-dependent lookup to be considered. For example, suppose there
      is a type "thing" in a namespace "things":

      namespace things {
      struct thing { ... };

      void swap (thing&, thing&);
      }

      Suppose you later want to swap two things. If you say:

      using std::swap;
      swap(thing1, thing2);

      The compiler has a chance to find things::swap. That's usually a good
      thing, since it may use a special thing-specific swapping technique,
      e.g. just swapping the things' p_impls.

      If instead you say:

      std::swap(thing 1, thing2);

      You will specifically get std::swap, and things::swap will not even be
      considered. This is not necessarily wrong, but it is probably sub-optimal.

      Comment

      • Juha Nieminen

        #4
        Re: Namespace inclusion

        Jeff Schwab wrote:
        namespace things {
        struct thing { ... };
        >
        void swap (thing&, thing&);
        }
        >
        Suppose you later want to swap two things. If you say:
        >
        using std::swap;
        swap(thing1, thing2);
        Actually I believe you don't need the 'using' there for the 'swap'
        (without the 'things::' prefix) to work. That's because the parameters
        are types defined inside the namespace. There's this funny lookup rule
        in C++.
        (This lookup rule exists so that you can write std::cout << str;
        instead of having to write std::operator<< (std::cout, str);)

        Comment

        • Jeff Schwab

          #5
          Re: Namespace inclusion

          Juha Nieminen wrote:
          Jeff Schwab wrote:
          > namespace things {
          > struct thing { ... };
          >>
          > void swap (thing&, thing&);
          > }
          >>
          >Suppose you later want to swap two things. If you say:
          >>
          > using std::swap;
          > swap(thing1, thing2);
          >
          Actually I believe you don't need the 'using' there for the 'swap'
          (without the 'things::' prefix) to work. That's because the parameters
          are types defined inside the namespace. There's this funny lookup rule
          in C++.
          (This lookup rule exists so that you can write std::cout << str;
          instead of having to write std::operator<< (std::cout, str);)
          You missed the point of the OP's post. It's true that in this case,
          there is a things::swap function. My answer was supposed to cover the
          general case in which you don't know whether such a function exists,
          which was (as I understood it) relevant to the original post.

          Comment

          • James Kanze

            #6
            Re: Namespace inclusion

            Jeff Schwab wrote:
            Juha Nieminen wrote:
            Jeff Schwab wrote:
            namespace things {
            struct thing { ... };
            void swap (thing&, thing&);
            }
            Suppose you later want to swap two things. If you say:
            using std::swap;
            swap(thing1, thing2);
            Actually I believe you don't need the 'using' there for the 'swap'
            (without the 'things::' prefix) to work. That's because the parameters
            are types defined inside the namespace. There's this funny lookup rule
            in C++.
            (This lookup rule exists so that you can write std::cout << str;
            instead of having to write std::operator<< (std::cout, str);)
            You missed the point of the OP's post. It's true that in this case,
            there is a things::swap function. My answer was supposed to cover the
            general case in which you don't know whether such a function exists,
            which was (as I understood it) relevant to the original post.
            To be fair to Juha, you didn't even mention the word template,
            and that's the only context where your solution might be
            necessary. In the case where you might end up with a basic
            type, which isn't in any namespace for ADL to pull in.

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

            • Jeff Schwab

              #7
              Re: Namespace inclusion

              James Kanze wrote:
              Jeff Schwab wrote:
              >Juha Nieminen wrote:
              >>Jeff Schwab wrote:
              >>> namespace things {
              >>> struct thing { ... };
              >
              >>> void swap (thing&, thing&);
              >>> }
              >
              >>>Suppose you later want to swap two things. If you say:
              >
              >>> using std::swap;
              >>> swap(thing1, thing2);
              >
              >> Actually I believe you don't need the 'using' there for the 'swap'
              >>(without the 'things::' prefix) to work. That's because the parameters
              >>are types defined inside the namespace. There's this funny lookup rule
              >>in C++.
              >> (This lookup rule exists so that you can write std::cout << str;
              >>instead of having to write std::operator<< (std::cout, str);)
              >
              >You missed the point of the OP's post. It's true that in this case,
              >there is a things::swap function. My answer was supposed to cover the
              >general case in which you don't know whether such a function exists,
              >which was (as I understood it) relevant to the original post.
              >
              To be fair to Juha,
              Apologies to Juha for any rudeness on my part.
              you didn't even mention the word template,
              and that's the only context where your solution might be
              necessary. In the case where you might end up with a basic
              type, which isn't in any namespace for ADL to pull in.
              I'm not sure what you're saying. Suppose a function is defined to swap
              two objects of a UDT:

              namespace things {

              struct thing {
              // ...
              };

              void swap(thing& a, thing& b) {
              // ...
              }
              }

              If client code explicitly calls std::swap(thing 1, thing2), the
              type-specific things::swap will not be considered during the resolution
              process; on the other hand, swap(thing1, thing2) will find things::swap
              through ADL. In general, if client code does not wants to use
              type-specific swap overloads where appropriate, but otherwise use the
              std::swap algorithm, the correct idiom is:

              using std::swap;

              swap(thing1, thing2);

              Am I missing anything?

              Comment

              Working...