Is std::unique_ptr required to know the full definition of T?

前端 未结 9 1369
小鲜肉
小鲜肉 2020-11-22 07:47

I have some code in a header that looks like this:

#include 

class Thing;

class MyClass
{
    std::unique_ptr< Thing > my_thing;
};


        
相关标签:
9条回答
  • 2020-11-22 07:48

    The compiler needs the definition of Thing to generate the default destructor for MyClass. If you explicitly declare the destructor and move its (empty) implementation to the CPP file, the code should compile.

    0 讨论(0)
  • 2020-11-22 07:52

    Just for completeness:

    Header: A.h

    class B; // forward declaration
    
    class A
    {
        std::unique_ptr<B> ptr_;  // ok!  
    public:
        A();
        ~A();
        // ...
    };
    

    Source A.cpp:

    class B {  ...  }; // class definition
    
    A::A() { ... }
    A::~A() { ... }
    

    The definition of class B must be seen by constructor, destructor and anything that might implicitely delete B. (Although the constructor doesn't appear in the list above, in VS2017 even the constructor needs the definition of B. And this makes sense when considering that in case of an exception in the constructor the unique_ptr is destroyed again.)

    0 讨论(0)
  • 2020-11-22 07:58

    It looks like current answers are not exactly nailing down why default constructor (or destructor) is problem but empty ones declared in cpp isn't.

    Here's whats happening:

    If outer class (i.e. MyClass) doesn't have constructor or destructor then compiler generates the default ones. The problem with this is that compiler essentially inserts the default empty constructor/destructor in the .hpp file. This means that the code for default contructor/destructor gets compiled along with host executable's binary, not along with your library's binaries. However this definitions can't really construct the partial classes. So when linker goes in your library's binary and tries to get constructor/destructor, it doesn't find any and you get error. If the constructor/destructor code was in your .cpp then your library binary has that available for linking.

    This is nothing to do with using unique_ptr or shared_ptr and other answers seems to be possible confusing bug in old VC++ for unique_ptr implementation (VC++ 2015 works fine on my machine).

    So moral of the story is that your header needs to remain free of any constructor/destructor definition. It can only contain their declaration. For example, ~MyClass()=default; in hpp won't work. If you allow compiler to insert default constructor or destructor, you will get a linker error.

    One other side note: If you are still getting this error even after you have constructor and destructor in cpp file then most likely the reason is that your library is not getting compiled properly. For example, one time I simply changed project type from Console to Library in VC++ and I got this error because VC++ did not added _LIB preprocessor symbol and that produced exact same error message.

    0 讨论(0)
  • 2020-11-22 07:59

    The full definition of the Thing is required at the point of template instantiation. This is the exact reason why the pimpl idiom compiles.

    If it wasn't possible, people would not ask questions like this.

    0 讨论(0)
  • 2020-11-22 07:59

    I was looking for a way to use the PIMPL idiom with std::unique_ptr. This guide is a great resource.

    In short, here's what you can do to make it work:

    my_class.h

    #include <memory>
    
    class Thing;
    
    class MyClass
    {
        ~MyClass(); // <--- Added
        std::unique_ptr< Thing > my_thing;
    };
    

    my_class.cpp

    MyClass::~MyClass() = default; // Or a custom implementation
    
    0 讨论(0)
  • 2020-11-22 08:04

    As for me,

    QList<QSharedPointer<ControllerBase>> controllers;
    

    Just include the header ...

    #include <QSharedPointer>
    
    0 讨论(0)
提交回复
热议问题