我刚刚听完了Scott Meyers关于C++ 0x的软件工程电台播客采访.大多数新功能对我来说都很有意义,我现在对C++ 0x感到兴奋,除了一个.我仍然没有得到移动语义 ......它们究竟是什么?
可能重复:
从函数返回unique_ptr
20.7.1.2 [unique.ptr.single]定义了这样的复制构造函数:
// disable copy from lvalue
unique_ptr(const unique_ptr&) = delete;
unique_ptr& operator=(const unique_ptr&) = delete;
Run Code Online (Sandbox Code Playgroud)
那么,为什么下面的代码编译得很好?
#include <memory>
#include <iostream>
std::unique_ptr< int > bar()
{
std::unique_ptr< int > p( new int(4));
return p;
}
int main()
{
auto p = bar();
std::cout<<*p<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我编译它像这样:
g++ -O3 -Wall -Wextra -pedantic -std=c++0x kel.cpp
Run Code Online (Sandbox Code Playgroud)
编译器:g ++版本4.6.1 20110908(Red Hat 4.6.1-9)
我有一个非常基本的问题:返回std::vector<A>使用std::move是否是一个好主意?例如:
class A {};
std::vector<A> && func() {
std::vector<A> v;
/* fill v */
return std::move(v);
}
Run Code Online (Sandbox Code Playgroud)
我应该返回std::map,std::list..等等......这样?