gpt4 book ai didi

c++ - 说 xvalues 具有身份并且是可移动的是正确的吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:08 25 4
gpt4 key购买 nike

根据这份文件:

http://www.stroustrup.com/terminology.pdf

  1. l 值具有同一性且不可移动。
  2. 公关值是可移动的,但没有身份。
  3. x 值具有同一性并且是可移动的。

关于这些我有几个问题。

一个。具有身份的 x 值的示例是什么?以下是不合法的:

Foo f;
&std::move(f);

我可以重载 Foo 类的 &-operator 并让它返回 this 以便以下内容变得合法:

&Foo(5);

但是像 Foo(5) 这样的 pr-values 不能有身份。还是对身份有更微妙的解释?

最佳答案

Stroutrup 的 The C++ Programming Language 中的以下内容可能会澄清这个问题:

void f(vector<string>& vs)
{
vector<string>& v2 = std::move(vs);// move vs to v2
// ...
}

Here, std::move(vs) is an xvalue: it
clearly has identity (we can refer to
it as vs), but we have explicitly given
permission for it to be moved from by
calling std::move() (§3.3.2, §35.5.1).
For practical programming, thinking in
terms of rvalue and lvalue is usually
sufficient.

关于c++ - 说 xvalues 具有身份并且是可移动的是正确的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com