I am learning C++ and in my book using namespace std is written in every program. I understand that std::cout <<"hello"; can be simply written as cout << "hello"; by using namespace std.

Why?

  • @gun
    link
    43 years ago

    Namespaces are a way of avoiding conflicting identifiers when you include multiple headers. It’s very easy to imagine that when you are using a bunch of different libraries, two of them will have a class or function with the same name, and without namespaces, things won’t compile.

    Now about using namespace std. There’s no good reason to do this outside of convenience. I would recommend never putting “using namespace std” in your headers. It opens up the possibility for a massive amount of identifiers in the std library to conflict with your own, so you might run into errors while you are coding.

    But, you can use the “using” keyword for a narrower scope. If you don’t want to type std::cout all the time, you can put “using std::cout;” and this has the same effect, without as much risk.

  • @Reaton
    link
    3
    edit-2
    2 years ago

    deleted by creator

  • @the_tech_beastOP
    link
    23 years ago

    I watched a video and it mentioned the purpose is to avoid naming conflicts which I didn’t understand.