21

Now first, I am aware of the general issues with unique_ptr<> and forward declarations as in Forward declaration with unique_ptr? .

Consider these three files:

A.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());
}

Issuing g++ -std=c++11 main.cpp C.cpp yields the following error:

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,

That's true, B is an incomplete type in line 6 of A.h - but that's not where A's destructor is! g++ seems to generate a destructor for A even though I am providing one. A's destructor is in C.cpp line 7 and there B is a perfectly defined type. Why am I getting this error?

Community
  • 1
  • 1
hllnll
  • 374
  • 1
  • 2
  • 8

2 Answers2

25

You also need to put A's constructor in C.cpp:

A.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() {

}

See this answer. The constructor needs access to the complete type as well. This is so that it can call the deleter if an exception is thrown during construction.

Community
  • 1
  • 1
Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • 1
    The same happens btw for the move constructor, if it were not implicitly defined as deleted because of the explicitly-declared destructor. (And the copy ctor is defined as deleted as well because `unique_ptr` cannot be copied.) – dyp Feb 07 '15 at 20:12
9

The implicitly defined special member functions are inline, leading to issues with incomplete types. As the link from Chris's answer shows, all non-delegating constructors could potentially invoke the destructor. This includes copy(deleted in this case) and move constructors as well. So, when you are dealing with non-static members involving incomplete types, explicitly default the definitions in the source file, thus ensuring that they aren't defined inline.

In header:

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

In source:

A::A() = default;
A::~A() = default;
A::A(const A&) = default;
A::A(A&&) = default;
Community
  • 1
  • 1
Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • You have two typos in the source file (missing `=`). Also, the copy ctor cannot be defaulted in this case. – dyp Feb 07 '15 at 22:19
  • I did mention the fact about the copy ctor in the answer. Fixed typo. Thanks! – Pradhan Feb 07 '15 at 22:41