c++ : comma separated list in using declaration

1.4k Views Asked by At

Why is it a bad practice to use a comma separated list in a using declaration statement in c++?

For example

using std::cout;
using std::cin;
using std::endl;

is considered better code than

using std::cout,std::cin,std::endl;

Some compiliers(gcc, for instance) even issue warnings if the code contains a comma separated list of using declarations.

Ofcourse, the best practice would be to use fully qualified names for identifiers.

2

There are 2 best solutions below

1
On BEST ANSWER

As documented at cppreference, the form

using declarator-list;

is available from C++17 onwards only.

Your compiler is probably telling you this, if you'd read the warning!

1
On

Approaching from the other side, Reasons for disapproving of horizontal lists:

  1. using std::cout; is about an inch wide on my screen. This allows the eye to read it with very little horizontal movement and the vertical distance traveled to the next line is much shorter than having to read one item and then the next and the next.... Most people can read the vertical scroll faster.

  2. Next, lined up vertically you tend to get more easily recognized patterns. A rogue stb:: will stand out better in a line-up of std::s to the majority of readers. Not much, but it may save you a compile and that compile might take a long, long time under some circumstances.

  3. Inserting, removing, or altering a using declaration will be easier to pick out in file difference output if it's on its own line. If the changes are buried all on line you have to scan both lines and run a Mark 1 Eyeball diff to find out what changed, and the Mark 1 Eyeball and the human CPU can really suck at doing this.