cppreference是否使用术语"[对象的身份]"? c ++ 11和c ++ 17有两个不同的含义吗? [英] Is cppreference using the term "[Object's] identity" is two different meanings for c++11 and for c++17?

查看:171
本文介绍了cppreference是否使用术语"[对象的身份]"? c ++ 11和c ++ 17有两个不同的含义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我已经完全理解了C ++ 17关于值类别的更改(在其他SO问题的帮助下,谢谢),但是现在我注意到了这个问题,这表明我不太了解它们

I thought I've managed to fully understand (with the help of other SO questions, thanks) the C++17's change regarding value categories, but now I've noticed this problem which suggests I don't really understand them.

在C ++ 11中,对值类别进行了具有身份/可以从中移出"的解释,并且

In C++11, there was a "has identity / can be moved from" interpretation of value categories and the definition of what "identity" means is still present in cppreference:

具有身份:可以通过比较对象的地址或它们标识的功能(直接或间接获得)来确定该表达式是否与另一个表达式引用相同的实体.

has identity: it's possible to determine whether the expression refers to the same entity as another expression, such as by comparing addresses of the objects or the functions they identify (obtained directly or indirectly).

在C ++ 17中,具有身份/可以从其移出"不再成立,但是新定义也基于身份"的概念:

In C++17, "has identity / can be moved from" is no longer true, but the new definition is also based on the concept of "identity":

glvalue(广义" lvalue)是一个表达式,其求值确定对象,位字段或函数的身份;

a glvalue ("generalized" lvalue) is an expression whose evaluation determines the identity of an object, bit-field, or function;

我的问题/误解是:这是身份"的相同意思,还是不同的身份"?据我了解,c ++ 17中的情况如下:

My question/misunderstanding is: is this the same meaning of "identity", or is it a different "identity"? The way I understand it, the situation in c++17 is as follows:

A f() { return A(); }

A a = f(); // 1: f() is a prvalue expression, used to directly initialize a.
f();       // 2: f() is a prvalue expr., converted to xvalue by temporary materialization
A&& r = f(); // 3: f() is a prvalue expr., converted to xvalue by temporary materialization

在第二和第三种情况下,我获得一个xvalue,这意味着它应该具有一个标识.因此,我应该能够获取其地址 我应该能够确定它是否与其他某些表达式引用相同的实体,但是我认为我不能.当然,在第三种情况下,我可以将& r"作为单独的命令执行,然后将其地址与另一个表达式的地址进行比较,但这是因为A&&是左值.正在通过A&&&获取地址在这种情况下,直接或间接获得"是什么意思?我认为这不是正确的答案,因为在C ++ 11中,我也可以轻松地做到这一点

In the 2nd and 3rd case, I obtain an xvalue, which means it should have an identity. So I should be able to get its address [edited:] I should be able to determine whether it refers to the same entity as some other expression, but I don't think I can. Of course, in the 3rd case I'm able to do "&r" as a separate command and then compare its address to the other expression's address, but that's because A&& is an lvalue. Is taking the address via A&& what they mean in this case by "obtained directly or indirectly"? I assumed this can't be the correct answer, because in C++11 I can also easily do

A&& r = f(); // 4: f() is a prvalue expression. Temporary materialization does
             // not happen, the temporary (prvalue) gets its lifetime 
             // extended to match the lifetime of the reference
&r;

,尽管扩展了r的寿命,但r引用的对象仍然是临时的,在c ++ 11中,temporary是prvalues.因此,我假设我可以绑定A&&对它(像任何左值一样,我可以接受一个地址,但只能在一个单独的表达式中)不足以得出其求值确定一个身份的结论(毕竟,&r;"行中发生的任何事情都不属于该行)对我原始表达的评估),但是在c ++ 17中,这似乎是唯一可能的解释?

and yet the object referenced by r is still a temporary despite having its lifetime extended, and in c++11 temporaries are prvalues. So I assume just the fact I can bind A&& to it (of which, like any lvalue, I can take an address but only in a separate expression) is not enough to conclude that its evaluation determines an identity (after all, whatever happens in the line "&r;" is not part of an evaluation of my original expression), and yet in c++17 it looks to me like this would be the only possible interpretation?

您能通过确定我写的内容的哪一部分不正确来帮助我吗?还是正确,答案仅仅是身份"一词改变了其含义?

Can you help me by determining which part of what I wrote is incorrect? Or is it correct, and the answer is simply that the word "identity" has changed its meaning?

推荐答案

为什么您认为C ++ 11概念不再适用?页面上说该版本引入了具有身份"的概念,而不是唯一使用它的版本. C ++ 17所做的事情是说prvalues等到"它们才用于对象初始化,从而完全成为对象. (因此,它将成为对象"与具有身份"合并在一起,这比较简单;显然,每个对象都可以检测到this.)

Why do you think that the C++11 concepts no longer apply? The page says that that version introduced the "has identity" idea, not that it’s the only version that uses it. What C++17 did was say that prvalues "wait until" they are used for initialization of an object to become an object at all. (It therefore merges "being an object" with "having identity", which is simpler; plainly every object can have this be detected.)

真正的意思是该目标对象的地址被无形地传递到prvalue结构的位置,以便它从一开始就出现在正确的位置.编译器已经以RVO的名称进行了此操作,但是更改保证了这一点,并删除了正式的可移动性限制.

What that really means is that the address of that target object is invisibly passed to the site of the prvalue construction so that it appears in the correct place from the start. Compilers were already doing that under the RVO name, but the change guaranteed that and removed formal movability restrictions.

xvalue肯定具有一个标识:如果通过(任何一种)引用将prvalue传递给函数,则它可以使用其地址.它甚至可以返回它,以便(在相同的全表达式期间)创建它的函数可以使用其地址.禁止使用临时地址是一项单独的安全措施,甚至在(例如)使用static_cast<A&&>(get_a())强制临时实现后也适用. (但是,这不能阻止您使用glvalue的地址,该glvalue引用的是从另一个函数返回的临时值.)

The xvalue certainly has an identity: if you pass a prvalue to a function by (either kind of) reference, it can take its address. It can even return it so that (during the same full-expression) the function that created it can use its address. The prohibition on taking the address of a temporary is a separate safety measure that applies even after (say) using static_cast<A&&>(get_a()) to force temporary materialization. (However, it can’t stop you from taking the address of a glvalue that refers to the temporary returned from another function.)

这篇关于cppreference是否使用术语"[对象的身份]"? c ++ 11和c ++ 17有两个不同的含义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆