"using namespace" question

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

    "using namespace" question

    It is typical to put the line:

    using namespace std;

    at the top of a file which makes use of std library objects. To take
    a simple example:

    #include <iostream>

    using namespace std;

    int main( ) {

    int not_std_namespa ce = 1;
    cout << "Hello World" << endl;
    cout << not_std_namespa ce << endl;
    return (0);
    }


    How does C++ know that the variable not_std_namespa ce is not in the
    std:: namespace? Or is it automatically put there? That is, because
    of the "using namespace" directive at the top of the file, references
    to "cout" are treated as "std::cout. " So why isn't
    "not_std_namesp ace" treated as "std::not_std_n amespace"? Or does C++
    first search the local namespace, and then search the std:: namespace?

    Thanks for any clarification,
    cpp
  • John Carson

    #2
    Re: &quot;using namespace&quot; question

    "cppaddict" <hello@hello.co m> wrote in message
    news:1iah80dls6 v41vbv9bpqva5lh j0b18kdq0@4ax.c om[color=blue]
    > It is typical to put the line:
    >
    > using namespace std;
    >
    > at the top of a file which makes use of std library objects. To take
    > a simple example:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main( ) {
    >
    > int not_std_namespa ce = 1;
    > cout << "Hello World" << endl;
    > cout << not_std_namespa ce << endl;
    > return (0);
    > }
    >
    >
    > How does C++ know that the variable not_std_namespa ce is not in the
    > std:: namespace? Or is it automatically put there? That is, because
    > of the "using namespace" directive at the top of the file, references
    > to "cout" are treated as "std::cout. " So why isn't
    > "not_std_namesp ace" treated as "std::not_std_n amespace"? Or does C++
    > first search the local namespace, and then search the std:: namespace?
    >
    > Thanks for any clarification,
    > cpp[/color]

    using namespace std;

    does not put you into namespace std. Instead it imports the names from
    namespace std into the current namespace, which is the global namespace in
    this example. To get into namespace std, you would call

    namespace std
    {
    //stuff
    }

    In your example, the following puts not_std_namespa ce into namespace std:

    #include <iostream>
    namespace std
    {
    int not_std_namespa ce = 1;
    }
    using namespace std;
    int main()
    {
    cout << "Hello World" << endl;
    cout << not_std_namespa ce << endl;
    // NOTE that the next line now compiles
    cout << std::not_std_na mespace << endl;
    return 0;
    }


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)

    Comment

    • cppaddict

      #3
      Re: &quot;using namespace&quot; question

      [color=blue]
      >does not put you into namespace std. Instead it imports the names from
      >namespace std into the current namespace, which is the global namespace in
      >this example.[/color]

      Ahh.... That is what I wanted to know. Thanks very much.

      cpp

      Comment

      • JKop

        #4
        Re: &quot;using namespace&quot; question

        Just don't put:

        using namespace std


        in a header file. The Header file gets parsed into the Source-code
        file that includes it, and thus, without the source-code man knowing,
        his entire Source-code file has brought in the stuff from std!

        So, I'd suggest, in the header files:

        void Hello(void)
        {
        std::cout << "Hello!!";
        }


        -JKop

        Comment

        • Michiel Salters

          #5
          Re: &quot;using namespace&quot; question

          cppaddict <hello@hello.co m> wrote in message news:<1iah80dls 6v41vbv9bpqva5l hj0b18kdq0@4ax. com>...[color=blue]
          > It is typical to put the line:
          >
          > using namespace std;
          >
          > at the top of a file which makes use of std library objects. To take
          > a simple example:
          >
          > #include <iostream>
          >
          > using namespace std;
          >
          > int main( ) {
          >
          > int not_std_namespa ce = 1;
          > cout << "Hello World" << endl;
          > cout << not_std_namespa ce << endl;
          > return (0);
          > }[/color]

          It's common, especially in small programs. As long as it's not used
          in headers, risks are pretty low.
          [color=blue]
          > How does C++ know that the variable not_std_namespa ce is not in the
          > std:: namespace? Or is it automatically put there? That is, because
          > of the "using namespace" directive at the top of the file, references
          > to "cout" are treated as "std::cout. " So why isn't
          > "not_std_namesp ace" treated as "std::not_std_n amespace"? Or does C++
          > first search the local namespace, and then search the std:: namespace?[/color]

          It's more the other way around. After the using directive, the compiler
          adds 'links' to all std:: members in the global namespace. References
          to cout resolve to this 'link', and the compiler follows it to get
          std::cout. The entry for 'not_std_namesp ace' is a real entry.

          The compiler must add links, because it needs to know that cout is
          actually in std::. The lookup for operator<< must find
          std::operator<< . The compiler knows it has to look in std::
          because of Argument Dependent Lookup (=Koenig lookup). Unqualified
          names are looked up also in the namespaces of their arguments.

          Regards,
          Michiel Salters

          Comment

          • fabio de francesco

            #6
            Re: &quot;using namespace&quot; question

            cppaddict <hello@hello.co m> wrote in message news:<1iah80dls 6v41vbv9bpqva5l hj0b18kdq0@4ax. com>...[color=blue]
            >
            > How does C++ know that the variable not_std_namespa ce is not in the
            > std:: namespace? Or is it automatically put there? That is, because
            > of the "using namespace" directive at the top of the file, references
            > to "cout" are treated as "std::cout. " So why isn't
            > "not_std_namesp ace" treated as "std::not_std_n amespace"? Or does C++
            > first search the local namespace, and then search the std:: namespace?
            >[/color]
            All C++ symbols are defined in a larger context called namespace.
            This is used to avoid name conflicts when a programmer wold define a
            function that has the same name in the C++ library.
            When a programmer writes 'using namespace std' is saying to the
            compiler to use all the symbols it finds in a program and that match
            with one in std namespace.
            Obviously it doesn't find any matching symbol for
            'my_no_std_func tion()' in the std namespace and so it looks for
            declarations and definitions in your program and your files included.
            When you want to define your own symbols that must be used instead of
            the ones in std namespace you must do the following:

            // in myheader.h
            namespace MyNameSpace
            {
            int funct()
            {
            ...
            }
            }

            // in main.cpp
            #include "myheader.h "
            ....
            using namespace std;
            ....
            int main()
            {
            using MyNameSpace::fu nct; // 'funct' without parenthesis
            ...
            funct(); // It uses MyNameSpace::fu nct()
            std::funct(); // It uses std::funct()
            ::funct(); // It's the same as before: std::funct()
            }

            Ciao, Fabio De Francesco.

            Comment

            Working...