C++0x函数返回值若是函数内创建的heap变量,则不再需要std::move函数来将其转成右值

测试代码如下:

#include <iostream>
 
class Obj
{
public:
    Obj()
    {
        std::cout << "Obj()" << std::endl;
    }
    Obj(Obj&&)
    {
        std::cout << "Obj(Obj&&)" << std::endl;
    }
    Obj(Obj const&)
    {
        std::cout << "Obj(Obj const&)" << std::endl;
    }
    Obj& operator=(Obj const&)
    {
        std::cout << "operator=(Obj const&)" << std::endl;
        return *this;
    }
    Obj& operator=(Obj&&)
    {
        std::cout << "operator=(Obj&&)" << std::endl;
        return *this;
    }
};
 
static Obj g_obj;
 
Obj func()
{
    Obj obj;
    return obj;
}
 
Obj func_static()
{
    return g_obj;
}
 
int main()
{
    std::cout << "normal:" << std::endl;
    auto o = func();
    std::cout << "static:" << std::endl;
    auto o2 = func_static();
    return 0;
}

输出为:

Obj()
normal:
Obj()
Obj(Obj&&)
static:
Obj(Obj const&)