加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 在头文件中的命名空间std中使用tr1 :: shared_ptr放置是否

发布时间:2020-12-16 05:04:35 所属栏目:百科 来源:网络整理
导读:使用std :: tr1 :: shared_ptr作为std :: shared_ptr在相应的头文件中使用指令是合法且良好的编程风格吗?像这样: namespace std{ using tr1::shared_ptr;} 我知道污染整个命名空间是不好的,但这种情况怎么样?有没有隐藏的陷阱?目标编译器是VS2008,但也需
使用std :: tr1 :: shared_ptr作为std :: shared_ptr在相应的头文件中使用指令是合法且良好的编程风格吗?像这样:
namespace std
{
   using tr1::shared_ptr;
}

我知道污染整个命名空间是不好的,但这种情况怎么样?有没有隐藏的陷阱?目标编译器是VS2008,但也需要与更高版本兼容.

解决方法

从技术上讲,如果您这样做,标准表示您进入未定义行为领域:

17.6.4.2.1命名空间std [namespace.std]

1 The behavior of a C++ program is unde?ned if it adds declarations or
de?nitions to namespace std or to a namespace within namespace std
unless otherwise speci?ed.

但在实践中,你很可能会逃脱它.哎呀,甚至Scott Meyers在Effective C 3rd Ed中提出了一个类似的未定义的命名空间别名技巧. (第54项,第268页)使用Boost功能作为缺少tr1功能的权宜之计.

namespace std { using namespace tr1 = ::boost; }

您的使用声明也是未定义的行为,但请继续并直接进入.

注意:在您的编译器版本和警告周围添加一个大胖警告,#define和#pragma注释它,并且一旦升级到实际具有std :: shared_ptr的编译器/库,请确保重新访问该头并删除码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读