Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Cancel
6
  • 749
    Don't forget you can do: "using std::cout;" which means you don't have to type std::cout, but don't bring in the entire std namespace at the same time.
    – Bill
    Commented Sep 21, 2009 at 15:29
  • 127
    It is particularly bad to use 'using namespace std' at file scope in header files. Using it in source files (*.cpp) at file scope after all includes is not quite as bad, as its effect is limited to a single translation unit. Even less problematic is using it inside functions or classes, because its effect is limited to the function or class scope.
    – sh-
    Commented Jun 25, 2017 at 14:37
  • 17
    I would discourage to use using directive but for specific namespaces like std::literals::chrono_literals, Poco::Data:Keywords,Poco::Units and stuff that will deal with literals or readability tricks. Whenever it is in header or implementation files. It might be OK in a function scope I guess, but apart from literals and stuff, it is not useful. Commented Jul 19, 2017 at 9:33
  • 25
    @Jon: It's got nothing to do with namespace std in particular. My emphasis was meant to be on "at file scope in header files". To put it as an advice: Do not use "using namespace" (std or other) at file scope in header files. It is OK to use it in implementation files. Sorry for the ambiguity.
    – sh-
    Commented Apr 9, 2018 at 14:10
  • 5
    I have no answer to add to the others, but I use: using [namespace]::[identifier]; in the local scope of relatively simple source files. For example, if I have a header frequently using the fully qualified: std::size_t type, I will typically prepend: using std::size_t; to the implementation. This simplifies reading the code, and only effects the local scope - that is: using std::[identifier]; employs the principle of least surprise. There are too many identifiers in the std namespace to keep track of. Later C++ revisions may add identifiers that clash with your own!
    – Brett Hale
    Commented Aug 7, 2021 at 13:34