首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“使用”(或其他机制)在unique_ptr中交换C++11中的auto_ptr?

“使用”(或其他机制)在unique_ptr中交换C++11中的auto_ptr?
EN

Stack Overflow用户
提问于 2015-07-27 06:19:20
回答 3查看 407关注 0票数 1

我正在用-std=c++11捕捉Cygwin下面的编译警告

代码语言:javascript
复制
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));
                                       ^

我试着补充:

代码语言:javascript
复制
#if defined(MYLIB_CXX11)
using auto_ptr = std::unique_ptr;
#else
using std::auto_ptr;
#endif

auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));

但是它导致了下面的结果,即使包括<memory>

代码语言:javascript
复制
$ 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;
                     ~~~~~^

我还尝试了以下几种变体:

代码语言:javascript
复制
#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中交换?还是有其他的机制?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-07-27 06:40:17

了解代码在您的控制下,可以使用或不支持C++11重新编译,因此可以为所需的智能指针( std::unique_ptrstd::auto_ptr)创建别名。

代码语言:javascript
复制
template <typename T>
struct local_ptr {
    #if defined(MYLIB_CXX11)
    typedef std::unique_ptr<T> ptr;
    #else
    typedef std::auto_ptr<T> ptr;
    #endif
};

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

代码语言:javascript
复制
local_ptr< PK_MessageAccumulator>::ptr managed = //...

语法比预期的要尴尬得多,但这是为了适应支持C++03的需求。

在所有情况下,长期的解决方案都是将auto_ptr的使用因素分解出来,或者对不推荐的警告保持沉默。

票数 2
EN

Stack Overflow用户

发布于 2015-07-27 11:48:41

我试着补充:

代码语言:javascript
复制
using auto_ptr = std::unique_ptr;
using std::auto_ptr;
auto_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));

但它的结果如下,即使包括在内。

代码语言:javascript
复制
cryptlib.cpp:770:27: error: no type named 'unique_ptr' in namespace 'std'

这是因为std::unique_ptr是一个template,而不是一个类型。相反,这应该有效。

代码语言:javascript
复制
#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和命令行工具)。

代码语言:javascript
复制
/usr/bin/c++ -std=c++11 -stdlib=libc++

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

票数 3
EN

Stack Overflow用户

发布于 2015-07-27 06:25:10

你有两个选择。

  • 在应用程序中使用auto_ptr,保持库的原样。弃用警告不会阻止应用程序正常工作,它们只是为了帮助。
  • 在应用程序中使用unique_ptr,并修改库以使用unique_ptr

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31646118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档