用于重复使用std :: xyz语句的C++宏

Jos*_* MN 3 c++ macros c-preprocessor

有时我的文件看起来像这样:

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::size_type;
Run Code Online (Sandbox Code Playgroud)

是否有可能以某种方式避免每次都写出公共部分?像这样的东西:

USING(std, cout, endl, string, vector, size_type);
Run Code Online (Sandbox Code Playgroud)

我在考虑一个var-arg宏,但不知道是否有可能迭代那些var args.

小智 7

你可以使用namespace你的代码,你需要std::cout,std::endl,std::...

例:

#include <iostream>

namespace my {
  using std::cout;
  using std::endl;
}

int main() {
  using namespace my;
  cout << "hello" << endl;
}
Run Code Online (Sandbox Code Playgroud)