Search code examples
c++syntaxnamespacesstdusing

concise syntax for c++ namespace


I wonder if it is possible to write the following lengthy namespace usage in a more succinct way:

#include <iostream>
#include <iomanip>

using std::ostream;
using std::cout;
using std::endl;
using std::ios;
using std::setw;
using std::setfill;
using std::hex;

say:

using std::{ostream,cout,endl,ios,setw,setfill,hex}; // hypothetically, of course

Solution

  • You may write

    using std::ostream, std::cout, std::endl, std::ios, std::setw, std::setfill, std::hex;
    

    provided that your compiler supports the Standard C++ 17.

    As for me then I advice to use qualified names instead of unqualified names introduced by using declarations. Unqualified names can confuse readers of the code and be reasons of ambiguities.

    For example if a reader of the code will meet the name hex he will be confused whether it is the standard manipulator std::hex or a user-defined name.

    Usually using declarations are used to introduce overloaded functions in a given scope or make visible names of members of base classes.