"using namespace" question

Discussion in 'C++' started by cppaddict, Apr 23, 2004.

  1. cppaddict

    cppaddict Guest

    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_namespace = 1;
    cout << "Hello World" << endl;
    cout << not_std_namespace << endl;
    return (0);
    }


    How does C++ know that the variable not_std_namespace 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_namespace" treated as "std::not_std_namespace"? Or does C++
    first search the local namespace, and then search the std:: namespace?

    Thanks for any clarification,
    cpp
    cppaddict, Apr 23, 2004
    #1
    1. Advertising

  2. cppaddict

    John Carson Guest

    "cppaddict" <> wrote in message
    news:
    > 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_namespace = 1;
    > cout << "Hello World" << endl;
    > cout << not_std_namespace << endl;
    > return (0);
    > }
    >
    >
    > How does C++ know that the variable not_std_namespace 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_namespace" treated as "std::not_std_namespace"? Or does C++
    > first search the local namespace, and then search the std:: namespace?
    >
    > Thanks for any clarification,
    > cpp


    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_namespace into namespace std:

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


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

  3. cppaddict

    cppaddict Guest


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


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

    cpp
    cppaddict, Apr 23, 2004
    #3
  4. cppaddict

    JKop Guest

    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
    JKop, Apr 23, 2004
    #4
  5. cppaddict <> wrote in message news:<>...
    > 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_namespace = 1;
    > cout << "Hello World" << endl;
    > cout << not_std_namespace << endl;
    > return (0);
    > }


    It's common, especially in small programs. As long as it's not used
    in headers, risks are pretty low.

    > How does C++ know that the variable not_std_namespace 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_namespace" treated as "std::not_std_namespace"? Or does C++
    > first search the local namespace, and then search the std:: namespace?


    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_namespace' 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::eek:perator<< . 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
    Michiel Salters, Apr 23, 2004
    #5
  6. cppaddict <> wrote in message news:<>...
    >
    > How does C++ know that the variable not_std_namespace 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_namespace" treated as "std::not_std_namespace"? Or does C++
    > first search the local namespace, and then search the std:: namespace?
    >

    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_function()' 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::funct; // 'funct' without parenthesis
    ...
    funct(); // It uses MyNameSpace::funct()
    std::funct(); // It uses std::funct()
    ::funct(); // It's the same as before: std::funct()
    }

    Ciao, Fabio De Francesco.
    fabio de francesco, Apr 23, 2004
    #6
    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. Peter van Merkerk

    Re: what does using namespace std mean?

    Peter van Merkerk, Aug 21, 2003, in forum: C++
    Replies:
    1
    Views:
    10,472
    soni29
    Aug 22, 2003
  2. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,358
    Howard
    Nov 29, 2004
  3. Andrew Ward
    Replies:
    5
    Views:
    292
    Andrew Ward
    Jul 25, 2005
  4. Schizoid Man

    Question about using namespace std

    Schizoid Man, Aug 22, 2006, in forum: C++
    Replies:
    3
    Views:
    344
    Jim Langston
    Aug 24, 2006
  5. Replies:
    7
    Views:
    420
    Jacek Dziedzic
    Oct 3, 2006
Loading...

Share This Page