I'm trying to use a unique_ptr
to derived class in a function that takes a unique_ptr
to a base class. Something like:
class Base {};
class Derived : public Base {};
void f(unique_ptr<Base> const &base) {}
…
unique_ptr<Derived> derived = unique_ptr<Derived>(new Derived);
f(derived);
If I understand this answer correctly, this code should work, but it causes the following compile errors:
error C2664: 'f' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to 'const std::unique_ptr<_Ty> &'
IntelliSense: no suitable user-defined conversion from "std::unique_ptr<Derived, std::default_delete<Derived>>" to "const std::unique_ptr<Base, std::default_delete<Base>>" exists
If I change f
to take unique_ptr<Derived> const &derived
, it works fine, but that's not what I want.
Am I doing something wrong? What can I do to work around this?
I'm using Visual Studio 2012.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…