问题：为什么右值引用unique_ptr<Base>&&能绑定到unique_ptr<Derived>对象？'#include <iostream>
#include <string>
#include <memory>

class StealingMethod
{
public:
    virtual ~StealingMethod() = default;

    void Steal()
    {
        auto target = PickTarget();
        std::cout << "The target has been chosen as the " << target << "." << std::endl;
        ConfuseTarget(target);
        StealTheItem(target);
    }

private:
    virtual std::string PickTarget() = 0;
    virtual void ConfuseTarget(const std::string&) = 0;
    virtual void StealTheItem(const std::string&) = 0;
};

class SubtleMethod : public StealingMethod
{
private:
    std::string PickTarget() override
    {
        return "shopkeeper";
    }

    void ConfuseTarget(const std::string& target) override
    {
        std::cout << "Approach the " << target << " with running tears and hug him!" << std::endl;
    }

    void StealTheItem(const std::string& target) override
    {
        std::cout << "While in close contact, grab the " << target << "'s wallet." << std::endl;
    }
};

class HitAndRunMethod : public StealingMethod
{
private:
    std::string PickTarget() override
    {
        return "old goblin woman";
    }

    void ConfuseTarget(const std::string& target) override
    {
        std::cout << "Approach the " << target << " from behind." << std::endl;
    }

    void StealTheItem(const std::string& target) override
    {
        std::cout << "Grab the handbag and run away fast!" << std::endl;
    }
};

class HalflingThief
{
public:
    // 最初unique_ptr按值传递的版本，正确且清晰
    // explicit HalflingThief(std::unique_ptr<StealingMethod> method) : method_{ std::move(method) } {  }
    explicit HalflingThief(std::unique_ptr<StealingMethod>&& method) : method_{ std::move(method) } {  }

    void Steal()
    {
        method_->Steal();
    }

    // 最初unique_ptr按值传递的版本，正确且清晰
    // void ChangeMethod(std::unique_ptr<StealingMethod> method)
    void ChangeMethod(std::unique_ptr<StealingMethod>&& method)
    {
        method_ = std::move(method);
    }

private:
    std::unique_ptr<StealingMethod> method_;
};

int main()
{
    // OK! Why???
    HalflingThief thief{ std::make_unique<SubtleMethod>() };
    thief.Steal();
    // OK! Why???
    thief.ChangeMethod(std::make_unique<HitAndRunMethod>());
    thief.Steal();

    // 一些更清晰的演示示例
    std::unique_ptr<SubtleMethod> up{ std::make_unique<SubtleMethod>() };
    // OK! 类型相同，引用可以绑定
    std::unique_ptr<SubtleMethod>& ref0 = up;
    // error!
    //std::unique_ptr<StealingMethod>& ref1 = up;
    // OK! Why???
    std::unique_ptr<StealingMethod>&& ref2 = std::move(up);

    return 0;
}

为什么右值引用 unique_ptr<Base>&& 可以绑定到 unique_ptr<Derived> 对象？

回答：简化你的问题描述：

```struct Children: Base {};

int main(int argc, char** argv) {    auto children = std::make_unique<Children>();    std::unique_ptr<Base>&& pb = std::move(children); // OK}```

首先std::move(children)的类型为std::unique_ptr<Children> &&，而pb的类型显然为 std::unique_ptr<Base>&&，显然这两个类型 并不严格匹配，那么以左边类型为准，产生一次构造，即：

```std::unique_ptr<Base>&& pb = std::unique_ptr<Base>(std::move(children));```

unique_ptr的一条构造函数为：

```template<class U, class E>unique_ptr<T>(unique_ptr<U, E>&& u) noexcept;```

这里的T为Base，U为Children，显然U*是可以隐式转换成T*的，编译通过。

> This constructor only participates in overload resolution if all of the following is true:a) unique_ptr<U, E>::pointer is implicitly convertible to pointer

由于非常左值引用不能绑定到右值上，自然如下语句非法：

```std::unique_ptr<Base>& pb = std::unique_ptr<Base>(std::move(children)); // FAILED```