0

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 ?

Help appreacited to find out what´s going on.

Mendes
  • 17,489
  • 35
  • 150
  • 263
  • 1
    Do note that doing `return std::move();` for an automatic-storage-duration object local to the function is perfectly superfluous. It's implicit, and called [(Named) Returned Value Optimization](https://en.wikipedia.org/wiki/Return_value_optimization) – JBL Mar 21 '16 at 17:21

1 Answers1

4

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*/);
}
TriskalJM
  • 2,393
  • 1
  • 19
  • 20
Jarod42
  • 203,559
  • 14
  • 181
  • 302