gpt4 book ai didi

c++ - unique_ptr 实现中可能存在的错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:00 27 4
gpt4 key购买 nike

<分区>

我试图将 unique_ptr 类成员与前向声明一起使用。正如许多消息来源所说,例如Forward declaration with 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,
^

这是为什么?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com