Dynamisches Casting für unique_ptr
Wie in Boost bietet C ++ 11 einige Funktionen für das Castingshared_ptr
:
std::static_pointer_cast
std::dynamic_pointer_cast
std::const_pointer_cast
Ich frage mich jedoch, warum es für keine äquivalenten Funktionen gibtunique_ptr
.
Betrachten Sie das folgende einfache Beispiel:
class A { virtual ~A(); ... }
class B : public A { ... }
unique_ptr<A> pA(new B(...));
unique_ptr<A> qA = std::move(pA); // This is legal since there is no casting
unique_ptr<B> pB = std::move(pA); // This is not legal
// I would like to do something like:
// (Of course, it is not valid, but that would be the idea)
unique_ptr<B> pB = std::move(std::dynamic_pointer_cast<B>(pA));
Gibt es einen Grund, warum dieses Verwendungsmuster entmutigt wird, und somit Funktionen, die denen in äquivalent sind?shared_ptr
sind nicht vorgesehenunique_ptr
?