14

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

Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
pingul
  • 3,351
  • 3
  • 25
  • 43
  • 3
    At some point, your `using`s become longer than the `std::`s you're allowing yourself to omit. – Barry Feb 16 '17 at 16:16
  • @Barry It all depends on how many times I use the included functions after ;) But yes, indeed! I usually only do this in mathy functions where everything tends to blend together visually after a while. Nice to get all those `::` out of the way. – pingul Feb 16 '17 at 16:19
  • 1
    Its not as good as what you are doing but at function scope you can use `using namespace std;`. Personally though I just use `std::` everywhere. – NathanOliver Feb 16 '17 at 16:20
  • 1
    Also note that `using` and `using namespace` have subtle differences in behaviour. [Example](http://ideone.com/eEWawy) – Simple Feb 16 '17 at 16:22
  • @NathanOliver Yes, I suggested it in the question as well. What I don't like is that it gives no context about the rest of the function because it's so broad. As it is it becomes a little more apparent what will come. – pingul Feb 16 '17 at 16:28
  • Don't worry about trivial concepts such as lines of code. Concentrate more on correctness and robustness. The only entity concerned with lines of code is the compiler, which doesn't really care (except for extremely large files). – Thomas Matthews Feb 16 '17 at 16:31
  • If typing is a concern, see if your IDE has keyboard macros. I usually type some character, such as #, then perform a regional (block) replace `#` with `std::`. – Thomas Matthews Feb 16 '17 at 16:32
  • @ThomasMatthews Thanks for your comment. I definitely agree with your sentiment—I tend to write have the normal `std::` scattered in the code, but I recently did some math with a lot of nested `std::sqrt(std::sin(...) + std::cos(...))...`, and I had problems debugging the code with so much clutter. The question is just a basis of surprise, as I would have expected it to work. Also, when scrolling through the file it's nice with limited visual clutter in the start of the functions. – pingul Feb 16 '17 at 16:42
  • 1
    Let me clarify, what I meant was that you could replace `#` with `using std::`. I've found this saves a lot of time for me. – Thomas Matthews Feb 16 '17 at 16:54
  • You know, you can put this on the same line, separated by semicolons. C++ doesn't care about newlines. – Nir Friedman Feb 16 '17 at 18:51

1 Answers1

14

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.

pingul
  • 3,351
  • 3
  • 25
  • 43