c++命名空间的简洁语法

concise syntax for c++ namespace

我想知道是否可以用更简洁的方式编写以下冗长的命名空间用法:

#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;

说:

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

你可以写

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

前提是您的编译器支持标准 C++ 17。

至于我,我建议使用限定名称而不是使用声明引入的非限定名称。不合格的名称可能会混淆 reader 的代码并成为歧义的原因。

例如,如果代码的reader会遇到名称hex,他会混淆它是标准操纵器std::hex还是用户定义的名称。

通常使用声明用于在给定范围内引入重载函数或使基类成员的名称可见类。