Instead of including a whole namespace in a given function, I like to only include the stuff I'm going to use, like:
void doStuff() {
using std::sin;
using std::cos;
...
// do stuff
}
Sometimes this list grows longer. I wanted to slim it down to the following (similar to how it's possible with variable declarations):
void doStuff() {
using std::sin, std::cos;
// do stuff
}
I was surprised to find that this was not possible
(error: expected ';' after using declaration
). Is there a reason for why the using
is defined this way? Is there another way to include a number of functions from a given namespace consisely (except doing using namespace ...;
)?
What I want does not seem to be possible currently. However, from 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)
With the following example:
namespace X {
using A::g, A::g; // (C++17) OK: double declaration allowed at namespace scope
}
This seems to suggest that it might be possible in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With