2

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
foehn
  • 431
  • 4
  • 13
  • 8
    Short answer: No. Is writting `std::` that big of a deal? It causes so many fewer problems when you just use the qualified names. – NathanOliver Feb 12 '20 at 16:02
  • 2
    ["Why is “using namespace std;” considered bad practice?"](https://stackoverflow.com/q/1452721/1171191) – BoBTFish Feb 12 '20 at 16:05

1 Answers1

4

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335