名称空间别名有什么意义? [英] what's the point of namespace aliases?

查看:87
本文介绍了名称空间别名有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在考虑如何为我的库创建一个强大的版本控制系统,并且想知道

命名空间别名究竟是什么意思是什么?这似乎是一种相当有限的方法。例如,

你根本就不能加载到一个别名的命名空间......这是一个简单的例子

我所说的

__________________________

#include< stdio.h>

//我的库的版本1

名称空间mylib_version_1 {

名称空间常量{

char const name [] =" My Library Version 1.0";

}

}

//版本1的我的库

命名空间mylib_version_2 {

命名空间常量{

char const name [] =" My Library Version 2.0"

}

}

//方法1:创建版本1的命名空间别名

命名空间mylib_alias = mylib_version_1;

//方法2:使用版本2创建命名空间

命名空间mylib_using {

使用命名空间mylib_version_2;

}

//无法添加到命名空间mylib_alias !!

//取消注释错误!

//为什么你不能这样做? ??

/ *

名字步mylib_alias {

class something_extra {

};

}

* /

//添加到命名空间mylib_using

命名空间mylib_using {

class something_extra {

};

}

//条目

int main(){

printf("方法1:%s \ n",mylib_alias :: constant ::名称);

printf("方法2:%s \ n",mylib_using :: constant :: name);

返回0;

}


_______________________

任何人都可以利用命名空间别名?我无法想到为什么我会在我的

版本控制系统的实现中选择使用''方法1'而不是''方法2''。特别是当您无法添加命名空间别名

时使用''方法1''!我知道我必须在这里遗漏一些东西......嗯......

解决方案

" Chris Thomasson" ,comp.lang.c ++:


任何人都可以利用命名空间别名?



嗯,你可以用它们来缩写;例如,当使用

Boost MPL库时,写起来很常见:


namespace mpl = boost:mpl;


" Pierre Senellart" < in ***** @ invalid.invalidwrote in message

news:et *********** @ nef.ens.fr ...
< blockquote class =post_quotes>
" Chris Thomasson" ,comp.lang.c ++:


>任何人都可以利用命名空间别名?



嗯,你可以用它们来缩写;例如,当使用

Boost MPL库时,写起来很常见:


namespace mpl = boost:mpl;



与此之间的区别是什么:


namespace mpl {

使用命名空间提升:: mpl;

}





我真的没看到命名空间的要点当我们已经拥有

时,别名更灵活,恕我直言,''使用命名空间''关键字...


" Chris Thomasson" ,comp.lang.c ++:


当我们已经拥有

时,我真的没有看到命名空间别名的主要内容灵活,恕我直言,''使用命名空间''关键字...



我认为它不太强大,因为你无法添加或覆盖

的东西,但有时你只想拥有一个别名,仅此而已,

命名空间别名语法非常适合这个角色...


你也可以用类似的方式争辩说,

从来没有理由这样做:


typedef std :: map< std :: string ,std :: stringmap;


,因为你有一个更强大的方法来使用继承:


类map:public std: :map< std :: string,std :: string {};


但是在这里,typedef更合适,因为它的意图是

程序员清楚。


I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:
__________________________
#include <stdio.h>
// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}
}
// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}
}
// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;
// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;
}
// can''t add to namespace mylib_alias!!
// uncomment for error!
// why can''t you do this???
/*
namespace mylib_alias {
class something_extra {
};
}
*/
// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};
}
// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;
}

_______________________
Anybody making good use out of namespace aliases? I can''t think of why I
would choose to use ''method 1'' over ''method 2'' in the implementation of my
versioning system. Especially when you can''t add to a namespace alias
created with ''method 1''! I know I must be missing something here... Humm...

解决方案

"Chris Thomasson" ,comp.lang.c++:

Anybody making good use out of namespace aliases?

Well, you can just use them for abbreviations; for instance, when using
Boost MPL library, it''s quite common to write:

namespace mpl=boost:mpl;


"Pierre Senellart" <in*****@invalid.invalidwrote in message
news:et***********@nef.ens.fr...

"Chris Thomasson" ,comp.lang.c++:

>Anybody making good use out of namespace aliases?


Well, you can just use them for abbreviations; for instance, when using
Boost MPL library, it''s quite common to write:

namespace mpl=boost:mpl;

what the difference between that and this:

namespace mpl {
using namespace boost::mpl;
}

?

I really don''t see the main point of namespace aliases when we already have
the more flexible, IMHO, ''using namespace'' keyword...


"Chris Thomasson" ,comp.lang.c++:

I really don''t see the main point of namespace aliases when we already have
the more flexible, IMHO, ''using namespace'' keyword...

I guess it is less powerful in the sense that you cannot add or override
things, but sometimes you just want to have an alias, nothing more, and
the namespace alias syntax is perfect for this role...

You could also argue in a similar way, that there is never a reason for
doing this :

typedef std::map<std::string,std::stringmap;

because you have a more powerful way to do this by using inheritance:

class map : public std::map<std::string,std::string{};

But here, the typedef is more appropriate because it makes the intent of
the programmer clear.


这篇关于名称空间别名有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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