我正在用-std=c++11捕捉Cygwin下面的编译警告
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中交换?还是有其他的机制?
发布于 2015-07-27 06:40:17
了解代码在您的控制下,可以使用或不支持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的使用因素分解出来,或者对不推荐的警告保持沉默。
发布于 2015-07-27 11:48:41
我试着补充:
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++标准库。
发布于 2015-07-27 06:25:10
你有两个选择。
auto_ptr,保持库的原样。弃用警告不会阻止应用程序正常工作,它们只是为了帮助。unique_ptr,并修改库以使用unique_ptr。你不能把它们混在一起。如果您不能修改库,那么您将被迫在代码中使用auto_ptr。
https://stackoverflow.com/questions/31646118
复制相似问题