cas*_*ini 5 c++ tr1 visual-studio-2008 visual-c++
在相应的标题中std::tr1::shared_ptr用作std::shared_ptr放置using指令是合法且良好的编程风格吗?像这样:
namespace std
{
using tr1::shared_ptr;
}
Run Code Online (Sandbox Code Playgroud)
我知道污染整个命名空间是不好的,但这种情况怎么样?有没有隐藏的陷阱?目标编译器是VS2008,但也需要与更高版本兼容.
从技术上讲,该标准规定,如果您执行以下操作,您将进入未定义行为领域:
\n\n17.6.4.2.1 命名空间 std [namespace.std]
\n\n\n\n\n1 如果 C++ 程序将声明或 de\xef\xac\x81nitions 添加到命名空间 std 或命名空间 std\n 中的命名空间,则其行为是 unde\xef\xac\x81ned,除非另有指定\xef\xac\x81ed 。
\n
但在实践中,你很可能会侥幸逃脱。哎呀,甚至 Scott Meyers 在《Effective C++ 第三版》中也提出了类似的未定义命名空间别名技巧。(第 54 项,第 268 页)使用 Boost 功能作为缺失tr1功能的权宜之计。
namespace std { using namespace tr1 = ::boost; }\nRun Code Online (Sandbox Code Playgroud)\n\n您的 using 声明也是未定义的行为,但请继续并直接跳入。
\n\n注意:用一个大警告对其进行注释,#define并#pragma围绕您的编译器版本和警告,一旦您升级到实际具有 的编译器/库std::shared_ptr,请确保重新访问该标头并删除代码。
| 归档时间: |
|
| 查看次数: |
635 次 |
| 最近记录: |