我可以使用命名空间中的一些但不是全部名称,而不必重复所有名称的完整范围吗?

Can I use some but not all names from a namespace without having to repeat the full scope for all of them?

在我的代码中有好几次我写了类似

的东西
// 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));

因为我不想让该命名空间中的任何名称可用,除了我列出的 5 个名称。

C++ 是否提供任何东西来使用所有这些名称而不必一遍又一遍地编写作用域的公共部分?

您可以使用 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;

如果别名足够短,您甚至可能根本不需要 using 声明。但除此之外,没有任何其他方便的方法可以从名称空间中提取少量符号而不提取所有内容。

C++ 无法使符号 xyz 从某些命名空间 ns 可见,而无需重复 using 声明。

你的问题的一个关键点似乎是你需要经常这样做。为了缓解这种情况,您可以在指定的命名空间中填充这些符号。

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;
}

一个using声明就是一个声明,所以我们创建了一个包含只有五个你想要的东西的声明的命名空间。现在,在每个范围内,您希望这五个对于非限定名称查找可见,您可以简单地添加

using namespace common_stuff;

并且只有 common_stuff 中的那五个符号会显示。