Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) using

 

 

 

 

 

using is a keyword to specify the a namespace(s) used or the namespace(s) of the data types used.

 

Use using-directives for transition, for foundational libraries (such as std), or within a local scope [1] Don't put a using-directive in a header file [2,3] Don't write a using-directive before an #include [3] Prefer using over typedef for defining aliases [4]

 

 

 

 

Example: using to specify the namespace(s) of the data types used

 

#include <iostream>
#include <string>

int main()
{
  using std::cout;
  using std::string;
  const string s = "Hello world";
  cout << s;
}

 

 

 

 

 

Example: using to specify the namespace(s) used

 

#include <iostream>
#include <string>

int main()
{
  using namespace std;
  const string s = "Hello world";
  cout << s;
}

 

 

 

 

 

Reference

 

  1. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 14.5. Advice. page 417: '[10] Use using-directives for transition, for foundational libraries (such as std), or within a local scope'
  2. Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 14.5. Advice. page 417: '[11] Don't put a using-directive in a header file'
  3. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Item 59: 'Don't write namespace usings in a header file or before an #include'
  4. C++ Core Guidelines item T.43: 'Prefer using over typedef for defining aliases'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict