函数中的多个 using 指令
Multiple using directives in function
我不想在给定函数中包含整个命名空间,而是只包含我将要使用的内容,例如:
void doStuff() {
using std::sin;
using std::cos;
...
// do stuff
}
有时这个列表会变长。我想将其精简为以下内容(类似于变量声明的可能方式):
void doStuff() {
using std::sin, std::cos;
// do stuff
}
我惊讶地发现这是不可能的
(error: expected ';' after using declaration
)。为什么 using
是这样定义的?有没有另一种方法可以一致地包含给定名称空间中的许多函数(除了 using namespace ...;
)?
我想要的目前好像不能实现。但是,从 en.cppreference.com:
A using-declaration with more than one using-declarator is equivalent to a corresponding sequence of using-declarations with one using-declarator.
(since C++17)
用下面的例子:
namespace X {
using A::g, A::g; // (C++17) OK: double declaration allowed at namespace scope
}
这似乎表明它可能在未来成为可能。
我不想在给定函数中包含整个命名空间,而是只包含我将要使用的内容,例如:
void doStuff() {
using std::sin;
using std::cos;
...
// do stuff
}
有时这个列表会变长。我想将其精简为以下内容(类似于变量声明的可能方式):
void doStuff() {
using std::sin, std::cos;
// do stuff
}
我惊讶地发现这是不可能的
(error: expected ';' after using declaration
)。为什么 using
是这样定义的?有没有另一种方法可以一致地包含给定名称空间中的许多函数(除了 using namespace ...;
)?
我想要的目前好像不能实现。但是,从 en.cppreference.com:
A using-declaration with more than one using-declarator is equivalent to a corresponding sequence of using-declarations with one using-declarator. (since C++17)
用下面的例子:
namespace X {
using A::g, A::g; // (C++17) OK: double declaration allowed at namespace scope
}
这似乎表明它可能在未来成为可能。