Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple using directives in function

Tags:

c++

syntax

using

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 ...;)?

like image 653
pingul Avatar asked Feb 16 '17 16:02

pingul


1 Answers

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.

like image 71
pingul Avatar answered Sep 27 '22 20:09

pingul