可以包装或合并单独的命名空间? [英] Possible to wrap or merge separate namespaces?

查看:149
本文介绍了可以包装或合并单独的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在,寻找说明我找不到他们 - 甚至搜索使用搜索项组合,分组,合并和包装我不想出任何东西。也许我误解了我之前看到的。



但是,从两个名称空间开始,我们没有一个特定的应用程序,这只是一个好奇心, ...

 命名空间a {int func(){return 1;}} 
namespace b {int func ){return 2;}}



我在寻找语法要么简单地将它们换成另一个名称 - - 事后 -
(是的,我知道我可以重写它在一个嵌套的方式)或将它们合并到一个新的空间。但是,我确实发现,如果我添加到一个命名空间很有效。

  namespace c {namespace a {int func2(){return 3;}}} 


int main(int argc,char ** argv)
{
int a = a :: func ); // normal case
int c = c :: a :: func2(); // wrap并添加到

// int c = c :: func2(); //不工作
// int d = a :: func2(); //不工作
}

问题是:



1)是否有将两个空格合并成一个新空格的语法?



2)

解决方案

您可以这样做:

 命名空间c 
{
使用命名空间a;
using namespace b;
}

但是如果a和b有相同名称的元素,能够从命名空间c。


中使用它们

I seem to recall seeing notes somewhere on a way to combine multiple namespaces into one.

Now, looking for said notes I am not finding them -- even searching using search terms combing, grouping, merging and wrapping I'm not coming up with anything. Maybe I misunderstood what I saw before. I don't have a specific application for this, it's just a curiosity and it's a bit contrived.

But, starting with two name spaces...

namespace a {int func() {return 1;}}
namespace b {int func() {return 2;}}

I was looking for syntax to either simply wrap them in another name -- after the fact -- (yes, I know I can rewrite it in a nested way) or merge them into one new space. But, I did find that I if I add to one of the namespaces that much works.

namespace c {namespace a{ int func2() {return 3;}} }


int main(int argc, char **argv)
{
    int a = a::func();          // normal case
    int c = c::a::func2();      // wrapped and added to

    //int c = c::func2();       // doesn't work
    //int d = a::func2();       // doesn't work
}

The question are:

1) is there syntax that just combines the two spaces into one new one?

2) is there a syntax to wrap the spaces without adding more to the subspaces?

解决方案

You can do this:

namespace c
{
    using namespace a;
    using namespace b;
}

But if a and b have elements with the same names, you won't be able to use them from namespace c.

这篇关于可以包装或合并单独的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆