unique_ptr实现中可能存在的错误
Possible bug in unique_ptr implementation
我试图使用带有前向声明unique_ptr类成员。正如许多消息来源所说,例如 与unique_ptr一起向前声明?声明非内联析构函数应该就足够了,但在VS2013和GCC 5.3.1中似乎不是这种情况。我没有测试其他编译器。
例:
#include <memory>
class B;
class A {
public:
//A();
~A();
private:
std::unique_ptr<B> b;
};
//class B { };
int main() {
A a;
}
只有在取消注释 ctor 声明或类 B
声明后,我才能使此代码编译。否则在VS2013上出现错误
error C2338: can't delete an incomplete type
关于 GCC 错误:
In file included from /usr/local/include/c++/5.3.0/memory:81:0,
from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]':
/usr/local/include/c++/5.3.0/bits/unique_ptr.h:236:17: required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = B; _Dp = std::default_delete<B>]'
main.cpp:5:7: required from here
/usr/local/include/c++/5.3.0/bits/unique_ptr.h:74:22: error: invalid application of 'sizeof' to incomplete type 'B'
static_assert(sizeof(_Tp)>0,
^
这是为什么呢?
类 A 的析构函数必须知道类 B 的定义。只要 A 的构造函数/析构函数的实现文件知道类 B 的定义,类 B 的前向声明就可以了。如果您的实现(隐式)在头文件中,则需要在头文件中定义 B。你可以从Herb Sutter学习Pimpl。
相关文章:
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- Visual Studio 2019在内置库中存在错误
- 以下 OpenCV 源代码中是否存在错误?(#ifdef 没有 #else)
- 在存在错误代码的情况下输出参数与 NRVO
- 为什么我的删除功能中存在错误
- 哪个编译器(如果有的话)在参数包扩展中存在错误
- 未知功能已经存在错误
- progam在std :: getline AM之后停止响应,代码中是否存在错误
- Netuseradd用户已经存在错误
- GCC 4.4.1是否存在错误,因为它不接受ctor初始值设定项中的注入类名
- Visual C++中的外部模板是否存在错误
- 类模板成员不存在错误
- 如果构造函数中存在错误,请停止进程
- purify中存在错误的内存泄漏
- C++中是否存在错误类型
- 来自 vtable 的未定义符号是否意味着接口和实现之间存在错误
- 为什么我遇到此代码的分段错误?此代码几乎没有运行,因此构造函数或复制构造函数中可能存在错误
- c ++ 11 参数包在 Apple LLVM 7.0.0 中存在错误行为,但适用于 GCC-5.1
- 文件输出中存在错误字符
- 在boost::mutex构造函数中同时写入同一内存区域可能存在错误