c++c++11unique-ptrmake-shared

dynamic_pointer_cast unexpected behaviour


I'm building a factory class where I need to return a unique_ptr to a BaseClass. The returned pointer is made of a DerivedClass object that is converted to a shared pointer using make_shared and then converted to the desired BaseClass pointer as:

#include "BaseClass.h"
#include "DerivedClass.h"

std::unique_ptr<BaseClass> WorkerClass::DoSomething()
{

      DerivedClass derived;

      // Convert object to shared pointer
      auto pre = std::make_shared<DerivedClass>(derived);

      // Convert ptr type to returned type
      auto ret = std::dynamic_pointer_cast<BaseClass>(ptr);

      // Return the pointer
      return std::move(ret);
}

I'm getting this compiler error on std::move

error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::nullptr_t'
1>          with
1>          [
1>              _Ty=rfidaccess::BaseClass
1>          ]
1>          nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(261): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::shared_ptr<_Ty>' to 'std::nullptr_t'
1>          with
1>          [
1>              _Ty=rfidaccess::BaseClass
1>          ]
1>          nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(337): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'rfidaccess::AARLocomotiveBaseClass' to 'std::nullptr_t'
1>          with
1>          [
1>              _Ty=rfidaccess::BaseClass
1>          ]
1>          nullptr can only be converted to pointer or handle types
1>c:\project\dev\traansite1r\traansite1rcommon\tag.cpp(393): error C2664: 'std::unique_ptr<_Ty>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'rfidaccess::AAREndOfTrainBaseClass' to 'std::nullptr_t'
1>          with
1>          [
1>              _Ty=rfidaccess::BaseClass
1>          ]
1>          nullptr can only be converted to pointer or handle types

I'm using VS2012.

Why is it using something different than declared (std::unique_ptr<BaseClass>) ?

Is dynamic_pointer_cast not returning a std::unique_ptr<BaseClass> to ret ?


Solution

  • std::shared_ptr is not convertible to unique_ptr.

    In your case, you just want the following:

    std::unique_ptr<BaseClass> WorkerClass::DoSomething()
          return std::make_unique<DerivedClass>(/*args*/);
    }