"using"(或其他机制)在 C++11 中将unique_ptr换成auto_ptr?

"using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

本文关键字:ptr unique 换成 auto 中将 using 其他 机制 C++11      更新时间:2023-10-16

我在Cygwin下用-std=c++11捕获编译警告:

cryptlib.cpp: In member function ‘virtual size_t PK_Signer::SignMessage(RandomNumberGenerator&, const byte*, size_t, byte*) const’:
cryptlib.cpp:770:41: warning: ‘auto_ptr’ is deprecated (declared at /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/backward/auto_ptr.h:87) [-Wdeprecated-declarations]
std::auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));
                                       ^

我试着添加:

#if defined(MYLIB_CXX11)
using auto_ptr = std::unique_ptr;
#else
using std::auto_ptr;
#endif
auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));

但结果如下,即使包括<memory>

$ make cryptlib.o 
c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -DCRYPTOPP_DISABLE_ASM -Wall -Wextra -pipe -c cryptlib.cpp
cryptlib.cpp:770:27: error: no type named 'unique_ptr' in namespace 'std'
    using auto_ptr = std::unique_ptr;
                     ~~~~~^

我还尝试了以下的变体:

#if defined(MYLIB_CXX11)
typedef std::unique_ptr<T> local_ptr<T>;
#else
typedef std::auto_ptr local_ptr;
#endif

我不能做一个干净的切换到unique_ptr,因为它是一个c++ 03库,而c++ 03缺乏unique_ptr。我不能允许在c++ 11下继续进行脏编译,因为干净编译是一道安全门,而治理将不允许库通过。(警告技巧是不可能的,因为这应该是简单易行的。警告技巧包括禁用警告)。

是否可以使用"using"来交换unique_ptr ?还是有其他的机制?

我试着添加:

using auto_ptr = std::unique_ptr;
using std::auto_ptr;
auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));

但结果如下,即使包含

cryptlib.cpp:770:27: error: no type named 'unique_ptr' in namespace 'std'

那是因为std::unique_ptrtemplate,而不是类型。相反,这应该可以工作

#if __cplusplus >= 201103L
  // with C++11 we have std::unique_ptr (and std::auto_ptr is deprecated)
  template<typename T>
  using auto_ptr = std::unique_ptr<T>;
#else
  // assuming C++03 when there is std::auto_ptr
  using std::auto_ptr;
#endif

(要求您在代码中只使用auto_ptr,而不是std::auto_ptr)。


当然,我假设您使用有效的c++编译器和标准库。在Mac OS上,你可以使用(你可能需要安装Xcode和命令行工具)

/usr/bin/c++ -std=c++11 -stdlib=libc++

调用clang c++编译器和clang c++标准库。

了解代码在您的控制之下,可以在有或没有c++ 11支持的情况下重新编译,可以为所需的智能指针(std::unique_ptrstd::auto_ptr)创建别名。

template <typename T>
struct local_ptr {
    #if defined(MYLIB_CXX11)
    typedef std::unique_ptr<T> ptr;
    #else
    typedef std::auto_ptr<T> ptr;
    #endif
};

然后在客户端代码中使用;

local_ptr< PK_MessageAccumulator>::ptr managed = //...

语法比期望的更笨拙,但这是为了适应支持c++ 03的需求。

在所有情况下,长期的解决方案是排除auto_ptr的使用,或者沉默不赞成的警告。

您有两个选择。

  • 在您的应用程序中使用auto_ptr,并保持库原样。弃用警告不会阻止应用程序正常工作,它们只是用来提供帮助的。

  • 在您的应用程序中使用unique_ptr,并修改库以使用unique_ptr

你不能把它们混合。如果您不能修改库,那么您将被迫在代码中使用auto_ptr