【问题标题】:Can't use std::unique_ptr<T> with T being a forward declaration不能将 std::unique_ptr<T> 与 T 一起使用为前向声明
【发布时间】:2015-02-07 19:07:59
【问题描述】:

首先,我知道 unique_ptr 和前向声明的一般问题,如 Forward declaration with unique_ptr?

考虑这三个文件:

啊.h

#include <memory>
#include <vector>

class B;

class A
{
public:
    ~A();

private:
    std::unique_ptr<B> m_tilesets;
};

C.cpp

#include "A.h"

class B {

};

A::~A() {

}

main.cpp

#include <memory>

#include "A.h"

int main() {
    std::unique_ptr<A> m_result(new A());
}

发出g++ -std=c++11 main.cpp C.cpp 会产生以下错误:

In file included from /usr/include/c++/4.8/memory:81:0,
                 from main.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]’:
/usr/include/c++/4.8/bits/unique_ptr.h:184:16:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = B; _Dp = std::default_delete<B>]’
A.h:6:7:   required from here
/usr/include/c++/4.8/bits/unique_ptr.h:65:22: error: invalid application of ‘sizeof’ to incomplete type ‘B’
  static_assert(sizeof(_Tp)>0,

没错,B 是 A.h 的第 6 行中的不完整类型——但这不是 A 的析构函数所在的位置! g++ 似乎为 A 生成了一个析构函数,即使我提供了一个。 A 的析构函数在 C.cpp 第 7 行,B 是一个完美定义的类型。为什么会出现此错误?

【问题讨论】:

    标签: c++ c++11 unique-ptr forward-declaration


    【解决方案1】:

    你还需要把A的构造函数放在C.cpp中:

    啊.h

    #include <memory>
    #include <vector>
    
    class B;
    
    class A {
    public:
         A();
        ~A();
    
    private:
        std::unique_ptr<B> m_tilesets;
    };
    

    C.cpp

    #include "A.h"
    
    class B {
    
    };
    
    A::~A() {
    
    }
    
    A::A() {
    
    }
    

    this answer。构造函数也需要访问完整的类型。这样可以在构造过程中抛出异常时调用删除器。

    【讨论】:

    • 如果由于显式声明的析构函数而未将其隐式定义为已删除,则移动构造函数也会发生同样的情况。 (并且复制 ctor 也被定义为已删除,因为无法复制 unique_ptr。)
    【解决方案2】:

    隐式定义的特殊成员函数是内联的,导致类型不完整的问题。正如Chris's answer 中的link 所示,所有非委托构造函数都可能调用析构函数。这包括复制(在这种情况下被删除)和移动构造函数。因此,当您处理涉及不完整类型的非静态成员时,请显式默认源文件中的定义,从而确保它们不是内联定义的。

    在标题中:

    A();
    ~A();
    A(const A&);
    A(A&&);
    

    来源:

    A::A() = default;
    A::~A() = default;
    A::A(const A&) = default;
    A::A(A&&) = default;
    

    【讨论】:

    • 源文件中有两个拼写错误(缺少=)。此外,在这种情况下不能默认复制 ctor。
    • 我确实在答案中提到了有关复制 ctor 的事实。修正错字。谢谢!
    猜你喜欢
    • 2015-07-23
    • 2019-11-07
    • 1970-01-01
    • 2016-01-17
    • 2012-07-18
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多