C ++ std:.auto_ptr或std :: unique_ptr(以支持多个编译器,甚至是旧的C ++ 03编译器)?
我正在尝试更新一些C ++代码,我想朝着更现代的代码(c ++ 11)迈进,但是我仍然需要使用一些较旧的编译器(与c ++ 03兼容)来编译代码.,因为受支持的平台限制.
I'm trying to update some C++ code, I'd like to move toward a more modern code (c++11), but I still need to compile the code with some older compilers (c++03 compliant), because of supported platform constraints.
我知道在C ++ 11编译器中已弃用std :: auto_ptr,但是由于较旧的编译器支持,我不能仅将它们替换为std :: unique_ptr.
I know in C++11 compilers std::auto_ptr is deprecated, but because of the older compiler support, I can't just replace them with std::unique_ptr.
是否有一种好的做法来处理这种旧的编译器支持,但开始转向C ++ 11"?
Is there a good practice to handle this "old compiler support, but start to move to C++11"?
如您所述,std :: auto_ptr<>已在C ++ 11中弃用(参考).
As you noted, std::auto_ptr<> has been deprecated in C++11 (Reference).
移至c ++ 11 std :: unique_ptr<>是正确的方法,正如赫伯·萨特(Herb Sutter)在 GotW89 :
Moving to c++11 std::unique_ptr<> is the right way, as also stated by Herb Sutter in GotW89:
- 与auto_ptr有何关系?
auto_ptr的最大特点是在C ++具有移动语义之前尝试创建unique_ptr.auto_ptr现在已弃用,不应在新代码中使用.
如果现有代码库中有auto_ptr,则有机会尝试对auto_ptr进行全局搜索和替换为unique_ptr;否则,请执行以下操作:绝大多数使用方式都一样,并且可能会暴露(作为编译时错误)或(静默地)修复您不知道的一两个错误.
还请注意,C ++ 17将删除std :: auto_ptr.
Please also note that C++17 is going to remove std::auto_ptr.
我认为解决您的问题可能有不同的方式,即正确"的解决方案.一个还取决于实际代码的编写方式.
一些选项是:
I think there may be different ways of solving your problem, the "right" one also depends on how your actual code is written.
A few options are:
使用boost :: unique_ptr
Use boost::unique_ptr
有条件地使用基于__cplusplus的auto_ptr或unique_ptr.
Conditionally use auto_ptr or unique_ptr based on __cplusplus.
class Myclass {
#if __cplusplus<201103L
std :: auto_ptr m_ptr;
#else
std :: unique_ptr m_ptr;
#endif
...
class Myclass {
#if __cplusplus < 201103L
std::auto_ptr m_ptr;
#else
std::unique_ptr m_ptr;
#endif
...
这会分散在您引用auto_ptr的每个地方,我不太喜欢.
如果您对std :: auto_ptr的所有引用都已经过typedef了(只需有条件地更改typedef),则可能看起来不太尴尬.
This will be scattered in every place where you reference auto_ptr, I don't really like it.
May be look less awkward if all your references to std::auto_ptr are already typedef'ed (just conditionally change the typedef).
有条件地使用使用和别名来定义"对象.auto_ptr(并在没有std ::名称空间的情况下进行引用).
Conditionally use using and aliasing to "define" auto_ptr (and reference it without std:: namespace).
#if __cplusplus<201103L
使用std :: auto_ptr;
#else
模板
使用auto_ptr = std :: unique_ptr;
#endif
#if __cplusplus < 201103L
using std::auto_ptr;
#else
template
using auto_ptr = std::unique_ptr;
#endif
缺点:您一直使用"auto_ptr",但是在c ++ 11中,它表示std :: unique_ptr.
真令人困惑...
Drawback: you keep using "auto_ptr", but in c++11 it means std::unique_ptr.
Really confusing...
可能比选项2更好:
反向使用别名,并首选unique_ptr名称.
Probably slightly better than option 2:
reverse using aliases and prefer unique_ptr name.
在您自己定义的模板智能指针类中包装std ::智能指针(有条件地为auto_ptr或unique_ptr).
这可能很麻烦,并且需要使用新类搜索并替换所有auto_ptr引用.
Wrap the std:: smart pointer (conditionally auto_ptr or unique_ptr) in your own defined template smart pointer class.
This may be cumbersome and requires search and replacement of all auto_ptr references with your new class.
其他选项涉及std ::名称空间内部的定义,我认为该定义已被标准禁止,
或使用预处理程序#define来... ehm ...重命名"仅适用于旧的C ++ 03编译器,将unique_ptr更改为auto_ptr.
Other options involve definitions inside the std:: namespace, which I think is prohibited by the standard,
or using preprocessor #define to ...ehm... "rename" unique_ptr to auto_ptr just for old C++03 compilers.