std::auto_ptr 是如何用右值初始化的?

How is std::auto_ptr initialized with a rvalue?

#include <iostream>
#include <string>
#include <memory>
#include <cstdlib>

std::string foo()
{
    return std::string("yyyyyyyyyyyyy");
}

void bar(std::string& s)
{
    std::cout << s << std::endl;
}


std::auto_ptr<std::string> foo1()
{
    bool x = std::rand() % 2;
    if (x) {
        return std::auto_ptr<std::string>(new std::string("eeeeeeeeeeee"));
    } else {
        return std::auto_ptr<std::string>(new std::string("aaaaaaaaaaaaa"));
    }
}


int main()
{
    //bar(foo());
    std::auto_ptr<std::string> a(foo1());    
}

注释行:bar(foo()) 无法编译,因为 bar 接受非常量引用和 foo returns 右值。但是带有 std::auto_ptr 的第二行编译。 std::auto_ptr 的复制构造函数也接受非常量引用。为什么它会编译?我在foo1中使用了std::rand()来消除RVO(Return值优化)。

这显然是有效的,因为使用了一个小技巧来释放指向构造 std::auto_ptr 的内部指针。 案例 4 来自 This Manual:

template< class Y >
auto_ptr( auto_ptr_ref<Y> m );

4) Constructs the auto_ptr with the pointer held in the auto_ptr instance referred to by m. p.release() is called for the auto_ptr p that m holds to acquire the ownership of the object. auto_ptr_ref is an implementation-defined type that holds a reference to auto_ptr. std::auto_ptr is implicitly convertible to and assignable from this type. The implementation is allowed to provide the template with a different name or implement equivalent functionality in other ways.

(强调)