小编Lai*_*ith的帖子

移动物体比复制更快的原因是什么?

我听过斯科特迈耶斯说" std::move()什么都不动"......但我还没明白这意味着什么.

所以要指出我的问题,请考虑以下事项:

class Box { /* things... */ };

Box box1 = some_value;
Box box2 = box1;    // value of box1 is copied to box2 ... ok
Run Code Online (Sandbox Code Playgroud)

关于什么:

Box box3 = std::move(box1);
Run Code Online (Sandbox Code Playgroud)

我确实理解左值和左值的规则,但我不明白的是记忆中实际发生了什么?它只是以某种不同的方式复制价值,共享地址或什么?更具体地说:什么使移动比复制更快?

我只是觉得理解这一点会让一切都清楚.提前致谢!

编辑:请注意,我不是在询问std::move()实现或任何语法的东西.

c++ move rvalue

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

decltype(rvalue expr)的类型推导规则是什么?

我观看了一段关于汽车和decltype的类型演绎规则的视频,由Scott Meyers解释......他解释了以下内容

// decltype(lvalue expr) => reference to the type of the expression
// decltype(lvalue name) => type of the name
Run Code Online (Sandbox Code Playgroud)

我理解这些规则......但他没有解释以下内容

// decltype(rvlaue expr) => ???
Run Code Online (Sandbox Code Playgroud)

所以我试着通过练习来理解它,所以我做了以下几点

int x = 8;
int func();  // calling this function is rvlaue expr ...

decltype(32) t1 = 128;    // Ok   t1 is int
decltype(64) t2 = x;      // Ok   t2 is int 
decltype(func()) t3 = x;  // Ok   t3 is int ... obviously
Run Code Online (Sandbox Code Playgroud)

现在神奇了

decltype(std::move(x)) t4 = x;  // Error t4 is …
Run Code Online (Sandbox Code Playgroud)

c++ decltype auto type-deduction

6
推荐指数
1
解决办法
399
查看次数

nullptr rvalue怎么样?

虽然看着实行nullptr 这里,有什么了我的注意的是,nullptr就是rvalue这意味着我们可以做这样的事情

std::nullptr_t&& nullref = nullptr;
Run Code Online (Sandbox Code Playgroud)

但怎么可能nullptrrvalue因为实现是这样的

const class {...} nullptr = {};
Run Code Online (Sandbox Code Playgroud)

这是核心功能吗?我错过了什么?

c++ rvalue nullptr language-lawyer

6
推荐指数
1
解决办法
505
查看次数

如何实现remove_reference

我正在学习类型特征和类型转换(修改?),所以我遇到了std::remove_reference. 我尝试像这样实现它:

template <class T>
struct remove_reference { typedef T type; };

template <class T>
struct remove_reference<const T> { typedef const T type; };

template <class T>
struct remove_reference<T&> { typedef T type; };

template <class T>
struct remove_reference<const T&> { typedef const T type; };
Run Code Online (Sandbox Code Playgroud)

现在当我使用它时:

remove_reference<int>::type x1;        // x1 is int : Ok
remove_reference<const int>::type x2;  // x2 is <type> : ???
remove_reference<int&>::type x3;       // x3 is int : Ok
remove_reference<const int&>::type x4; // x4 is …
Run Code Online (Sandbox Code Playgroud)

c++ types type-traits

5
推荐指数
1
解决办法
3767
查看次数

为什么rvalue/const引用有内存地址和大小?

我到处读到引用不是对象,它们只是别名,它们在内存中没有位置

int x = 256;
int& rx = x;

std::cout << x << " " << &x << std::endl;       // Output: 256  0x15FAB0
std::cout << rx << " " << &rx << std::endl;     // Output: 256  0x15FAB0

// seems legit ... fair enough ...
Run Code Online (Sandbox Code Playgroud)

现在考虑以下内容

const int& r1 = 8;      // lvalue ref to const int
int&& r2 = 32;          // rvlaue ref to int
const int&& r3 = 128;   // rvalue ref to const int

std::cout << r1 << …
Run Code Online (Sandbox Code Playgroud)

c++ reference

3
推荐指数
1
解决办法
436
查看次数

为什么不转发迭代器有附加赋值运算符?

我知道std::forward_list<T>::iterator没有复合赋值运算符(operator+=).但那是为什么呢?

我问这个有三个原因:

  1. 这个运算符不会推进"前进"迭代器operator++()吗?
  2. 是不是有一个帮助函数std::advance()做同样的事情?
  3. 我正在实施我自己的转发列表(用于学习),我想知道什么是错的operator+=().

c++ iterator forward-list

3
推荐指数
2
解决办法
151
查看次数

如何推断出通用引用

有一些事情Universal references让我很困惑,我知道

T&& + lvalue => T&
T&& + rvalue => T&&
Run Code Online (Sandbox Code Playgroud)

然后我听到Scott Meyers说:"普遍引用的特殊规则"是:

T&& + rvalue => T (NOT T&&)
Run Code Online (Sandbox Code Playgroud)

所以我做了简单的测试思考,它会让事情变得清晰

int i = 32;
auto&& v = std::move(i); // assigning xvalue

// According to Scott: auto&& is URef
// And according to the "rule" v should get deduced to T
// VS2015 tells me this is int&& (NOT int)... huh???

auto&& x = int(8); // assigning prvalue
// same as above ... hmmm
Run Code Online (Sandbox Code Playgroud)

所以我在这里失踪了......我真的需要理解这一点......

URef …

c++ type-deduction

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