简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM