rws*_*wst 5 c++ macros unique-ptr c++11
在仍然使用 C++11 之前的项目中,我想通过使用 C++11 编译器进行编译并修复错误来为开关准备源代码。他们包括
std::auto_ptr<T>替换为的实例std::unique_ptr<T>std::move()0并NULL替换为nullptr现在我想切换回 C++ 之前的编译器并编写一个可以切换回更改的宏,以便在进行最终编译器切换时,我只需删除该宏。我试过
#ifndef HAVE_CXX11
#define nullptr NULL
namespace std {
#define unique_ptr<exvector> auto_ptr<exvector>
}
#endif
Run Code Online (Sandbox Code Playgroud)
(exvector使用与智能指针一起使用的示例类型)这种和类似的尝试不起作用,因为宏无法更改模板类型。我也用过typedef,没有更好的结果。
这有可能吗,如果有,怎么做?
对于nullptr和unique_ptr,这可以工作:
#ifndef HAVE_CXX11
#define nullptr NULL
#define unique_ptr auto_ptr
#endif
Run Code Online (Sandbox Code Playgroud)
但我不知道你打算如何处理 和 的不同unique_ptr语义auto_ptr。
如果您愿意忍受一些未定义的行为一段时间(不太可能导致实际问题的类型),您也可以提供您自己的std::move:
namespace std {
template <class T>
T& move(T &x) { return x; }
}
Run Code Online (Sandbox Code Playgroud)
它是 UB,因为你不允许向 中添加任何内容namespace std。但如果这只是一个临时措施,那么应该是安全的(11 之前的编译器不太可能有这个名字std::move)。