Polymorphic unique_ptr instance

I have the following code that works on Clang 5.0, but not on Clang 3.8, with C ++ 14 enabled:

class Base {};
class Derived : public Base {};

std::unique_ptr<Base> MakeDerived()
{
    auto derived = std::make_unique<Derived>();
    return derived;
}

int main()
{
    auto base = MakeDerived();
    std::cout << "Type: " << typeid(base).name() << '\n';
}

Live example here

Is the return value technically moving in this case due to copying? And if so, does this mean that the move constructor is unique_ptrdesigned to support implicit type enhancements of custom classes? It is hard to say from the cppreference documentation (# 6) unless I assume that this documentation page assumes that the type of the template is Uclearly different from the type Tfor the class itself.

, , , unique_ptr , rvalue std::move(), , . , clang 3.8, -std=c++14, , , - ( , ), v3.8 .

+4
2

elision ( ++ 17) , , .

, , ++ ( ++ 11) (12.8/32), : " , rvalue , ."

++ 11 " " "", , ( cv).

++ 14 " " , " elision , ".

, ++ 11 , std::unique_ptr<Derived> ( derived) std::unique_ptr<Base> ( ), std::move .

++ 14 , derived rvalue. , , :

std::unique_ptr<T> r std::unique_ptr<U>, U* T*.

Clang 3.8, , ++ 11; , ++ 14 .


( , , , " " U T. : , # 6 - ).

+4

return unique_ptr , template<class U, class E> unique_ptr(unique_ptr<U, E>&&). ; derived, 1579.

+2

Source: https://habr.com/ru/post/1691742/


All Articles