"using"(或其他机制)在 C++11 中将unique_ptr换成auto_ptr?
"using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?
我在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_ptr
是template
,而不是类型。相反,这应该可以工作
#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_ptr
或std::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
。
- 为什么 std::unique 不调用 std::sort?
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 生成"unique"矩阵
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 我对 std::unique(算法)C++有问题
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++中的指针否定 (!ptr == NULL)
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误