[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppUsing
You probably shouldn't use namespace std in your code, because it pollutes the global namespace [1,2].
Don't write namespace usings in a header file or before an #include [3].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppUsing
(C++) using
Keyword that enables you to change the namespace in use. The most common namespace to be in is namespace std. You can set the namespace using 'using namespace' or call the namespace and use the scope operator, ::).You probably shouldn't use namespace std in your code, because it pollutes the global namespace [1,2].
Don't write namespace usings in a header file or before an #include [3].
Hello world program using 'using namespace'
using namespace std; int main() { cout <<"Hello World" << endl; return 0; }
- include <iostream>
Hello world program using scope operator
int main() { std::cout <<"Hello World" << std::endl; return 0; }
- include <iostream>
Code links
- ::, scope operator
- <<, stream out operator
- #include
- cout
- endl
- include
- int
- iostream
- main
- namespace
- return
- scope operator, ::
- std
- std::cout
- std::endl
- stream out operator , <<
'using' links
References
- 1) C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 .Item 27.5: 'Should I use using namespace std in my code? Probably not.'
- 2) Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'
- 3) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 59: 'Don't write namespace usings in a header file or before an #include'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
