mercredi 2 novembre 2016

"Use of undefined type" with unique_ptr to forward declared class and defaulted move constructor/assignment

In the following code, is the only way to avoid a compile error and to include B.h implementing the move constructor / assignment manually in A.cpp?

// A.h
#include <memory>
class B; // implementation somewhere in B.h/B.cpp
class A
{
public:
    A() = default;
    ~A() = default;
    A(const A&) = delete;
    A& operator=(const A&) = delete;
    A(A&&) = default;
    A& operator=(A&&) = default;

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

Visual Studio 2015 gives "error C2027: use of undefined type", since the move constructor/assignment operator of std::unique_ptr calls the deleter on m_b (trying to invoke B's destructor), which is obviously not known at this point.

Aucun commentaire:

Enregistrer un commentaire