相关疑难解决方法(0)

什么是移动语义?

我刚刚听完了Scott Meyers关于C++ 0x的软件工程电台播客采访.大多数新功能对我来说都很有意义,我现在对C++ 0x感到兴奋,除了一个.我仍然没有得到移动语义 ......它们究竟是什么?

c++ c++-faq move-semantics c++11

1614
推荐指数
11
解决办法
39万
查看次数

为什么我可以复制unique_ptr?

可能重复:
从函数返回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)

c++ g++ unique-ptr c++11

23
推荐指数
2
解决办法
2万
查看次数

用std :: move返回std :: vector

我有一个非常基本的问题:返回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..等等......这样?

c++ return move

9
推荐指数
1
解决办法
6219
查看次数

标签 统计

c++ ×3

c++11 ×2

c++-faq ×1

g++ ×1

move ×1

move-semantics ×1

return ×1

unique-ptr ×1