1

Several times in my code I write something like

// all these using
using this::is::a::very::long::name::space::var1;
using this::is::a::very::long::name::space::var2;
using this::is::a::very::long::name::space::var3;
using this::is::a::very::long::name::space::fun1;
using this::is::a::very::long::name::space::fun2;
// to make this line cleaner
fun1(var1,fun2(var2,var3));

because I don't want to make any name from that namespace available, other then the 5 names I've listed.

Does C++ offer anything to use all those names without having to write the common part of the scope over and over?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    Does this answer your question? [In C++, what is a "namespace alias"?](https://stackoverflow.com/questions/1211399/in-c-what-is-a-namespace-alias) – IlCapitano Sep 14 '20 at 14:21
  • 2
    I'm not so sure that this is a duplicate. This isn't about shortening the namespace, it's asking about a facility for reducing the number of `using` declarations in the same way that python has a `from package import x, y, z`. C++ has no such facility, but this doesn't really make the question a duplicate of "What is a namespace alias" – Human-Compiler Sep 14 '20 at 14:32
  • I saw some macro-fu using a sophisticated macro function that would allow you to do `USING(this::is::a::very::long::name::space, var1, var2, var3, fun1, fun2)` ... but I thought the "cure" was worse than the problem it was trying to solve (because I hate macros). – Eljay Sep 14 '20 at 15:20

2 Answers2

3

A key point in your question seems to be that you need to do it often. To alleviate that, you can prime those symbols in a designated namespace.

namespace common_stuff {
  // You can also use an alias as the other answer suggests to shorten this
  using this::is::a::very::long::name::space::var1;
  using this::is::a::very::long::name::space::var2;
  using this::is::a::very::long::name::space::var3;
  using this::is::a::very::long::name::space::fun1;
  using this::is::a::very::long::name::space::fun2;
}

A using declaration is a declaration, and so we created a namespace that contains only five declaration for the things you want. Now, in every scope you want those five to be visible for unqualified name lookup, you can simply add

using namespace common_stuff;

And only those five symbols from common_stuff will be made visible.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

You could use a namespace alias:

// Create an alias namespace of the long namespace
namespace shorter = this::is::a::very::long::name::space;

using shorter::var1;
using shorter::var2;
using shorter::var3;
using shorter::fun1;
using shorter::fun2;

If you make the alias short enough, you may not even need the using declarations at all. But otherwise there isn't any other convenient way to pull in a small amount of symbols from a namespace without pulling in everything.

C++ lacks any way to make symbols x,y,z visible from some namespace ns without requiring the repeated using declarations.

Human-Compiler
  • 11,022
  • 1
  • 32
  • 59
  • 1
    Mmm, this seems a _nice to have_ feature. Do you know if they are considering it in some way? – Enlico Sep 14 '20 at 14:29
  • I agree that this would be a nice to have feature. I took a quick look through some of the recent/open proposals [here](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/), but I did not see anything explicitly for it (though I may have missed it). I would be surprised if it hasn't at least been proposed once. – Human-Compiler Sep 14 '20 at 14:42