- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
左值是绑定(bind)到内存的确定区域的值,而右值是一个表达式值,其存在是暂时的,不一定指向内存的确定区域。每当在需要右值的位置使用左值时,编译器都会执行左值到右值的转换,然后继续求值。
http://www.eetimes.com/discussion/programming-pointers/4023341/Lvalues-and-Rvalues
每当我们构造一个临时(匿名)类对象或从函数返回一个临时类对象时,虽然该对象是临时的,但它是可寻址的。但是,该对象仍然是有效的右值。这意味着该对象是 a) 可寻址的右值或 b) 当编译器期望使用左值时,它正在从左值隐式转换为右值。
例如:
class A
{
public:
int x;
A(int a) { x = a; std::cout << "int conversion ctor\n"; }
A(A&) { std::cout << "lvalue copy ctor\n"; }
A(A&&) { std::cout << "rvalue copy ctor\n"; }
};
A ret_a(A a)
{
return a;
}
int main(void)
{
&A(5); // A(5) is an addressable object
A&& rvalue = A(5); // A(5) is also an rvalue
}
我们还知道函数返回(在以下情况下为a
)的临时对象是左值,如这段代码:
int main(void)
{
ret_a(A(5));
}
产生以下输出:
int 转换函数
左值复制ctor
表示使用实参A(5)
调用函数ret_a
调用转换构造函数A::A(int)
它用值 5 构造函数的形式参数 a
。
当函数完成执行后,它会构造一个临时的A
对象,使用a
作为参数,调用A::A(A&)
。但是,如果我们要从重载构造函数列表中删除 A::A(A&)
,返回的临时对象仍然会匹配右值引用构造函数 A::A(A&&)
.
这是我不太理解的:对象 a
如何同时匹配右值引用和左值引用?很明显 A::A(A&)
比 A::A(A&&)
更好的匹配(因此 a
必须是一个左值)。但是,由于不能将右值引用初始化为左值,鉴于形式参数 a
是左值,它应该无法匹配对 A::A(A&&) 的调用
。如果编译器正在进行左值到右值的转换,那将是微不足道的。事实上,从 'A' 到 'A&' 的转换也是微不足道的,两个函数应该具有相同的隐式转换序列等级,因此,当两个 A::A(A&)
和 A::A(A&&)
在重载函数候选集中。
此外,问题(我之前问过的)是:
给定对象如何同时匹配右值引用和左值引用?
最佳答案
对我来说:
int main(void)
{
ret_a(A(5));
}
产量:
int conversion ctor
rvalue copy ctor
(即右值,而不是左值)。这是您的编译器中的一个错误。然而,这是可以理解的,因为这种行为的规则仅在几个月前(2010 年 11 月)发生了变化。更多内容请参见下文。
When the function completes execution, it then constructs a temporary
A
object usinga
as its argument, which invokesA::A(A&)
.
其实没有。当函数 ret_a
完成执行时,它会使用 a
作为参数构造一个临时的 A
对象,该对象调用 A:A( A&&)
。这是由于 [class.copy]/p33] 1 :
When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided. — end note ]
但是,如果您删除 A::A(A&&)
构造函数,则将选择 A::A(&)
作为返回值。尽管在这种情况下,参数 a
的构造将失败,因为您无法使用右值构造它。但是暂时忽略这一点,我相信您的最终问题是:
How can a given object match both an rvalue reference and an lvalue reference?
在引用语句中:
return a;
答案在上面引用的标准草案段落中:尝试第一次重载解析,就好像 a
是一个右值一样。如果失败,则使用 a
作为左值再次尝试重载解析。这种两阶段过程仅在允许复制省略的上下文中尝试(例如返回语句)。
C++0x 草案最近已更改为在返回按值传递的参数时允许两阶段重载解析过程(如您的示例中所示)。这就是我们看到的不同编译器行为不同的原因。
关于c++ - 类、右值和右值引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266783/
我想知道最终用户按下了什么,所以我使用了 getch() 。 如果用户按右,我可以获得0xE0 0x4D。 如果用户按下Ctrl+右,我可以获得0xE0 0x47。 如果用户按下Shift+右,我可以
我已经构建了一个应用程序来搜索我的位置。 这是代码 var map; var gdir; var geocoder = null; var addressMarker; function init
我想为我的元素设计布局 View 。布局 View 在左 Angular 和右 Angular (同一行)有一个图像,将有 2 行单词,一行在第 1 行,另一行在第 2 行。我该如何实现? It
我有一个很长的线性(分支不多)流程图,在 graphviz 中显示为要么太高而无法放在单个页面上,要么太宽(如果方向是从左到右) 是否有一种简单的方法可以让 graphviz 以从左到右,然后向下,然
我一直摸不着头脑,但运气不好。设计器有一个包含 3 栏的站点、两个侧边栏和一个主要内容区域。 专为桌面设计,左栏、主要内容、右栏。但是,在较小的设备上,我们希望首先堆叠主要内容。 所以通常情况下,你可
我想要从上到下和从左到右组织的 css block 。 为了更好地解释这是一张图片,其中包含我到目前为止所获得的内容以及我希望使用 CSS 实现的内容: 代码如下: HTML: 1 2 3 4 5
当我问this question时,答案之一(现已删除)建议Either类型对应Curry-Howard correspondence中的XOR而不是OR,因为它不能同时是Left和Right。 真相
如果一行中六个观察值中至少有三个是 != NA,我想计算该行的平均值。如果存在四个或更多 NA,则平均值应显示为 NA。 给出平均值的例子,忽略了 NA: require(dplyr) a % mut
我有一个由 9 列组成的数据框,其中包含一个因素 list 。每行可以填充所有 9 列(因为在该行中包含 9 个“事物”),但大多数没有(大多数有 3-4 个)。列也不是特定的,就像第 1 列和第 3
这是我第一次尝试使用 R 构建函数。基本上我的预期目标如下。 使用 RoogleVision 包与 Google Cloud Vision API 通信 函数遍历目录中的图片 从每张图片的 Googl
使用: mean (x, trim=0.05) 从分布的每一侧移除 2.5%,这对于对称的双尾数据来说很好。但是如果我有一个尾部或高度不对称的数据,我希望能够只删除分布的一侧。有没有这个功能,还是我自
我想保留重复的列,并删除唯一的列。这些列将具有相同的值,但名称不同。 x1 = rnorm(1:10) x2 = rnorm(1:10) x3 = x1 x4 = rnorm(1:10) x5 = x
是否可以使WPF工具栏中的元素的Right水平对齐方式正确? 我尝试将内部元素添加到Grid中,并将ColumnDefinition分配给Left / Right。我
datatable(head(iris)) 如何将我的列居中,使其位于我的列名称的正下方? 最佳答案 您可以使用options 下的columnDefs 自变量。将 className 设置为 dt-
我是 R 的新手,但我正在尝试在 R 中制作滑动窗口。 使用循环我可以像这样,但这变得非常低效。 results=c(1:7) letters=c("A","B","C","D","E","F","G
假设我有这个 .txt 文件: here is line 1 here is line 2 here is line 3 here is line 4 我想将此字符串粘贴到第 3 行和第 4 行之间:
假设我有这个 .txt 文件: here is line 1 here is line 2 here is line 3 here is line 4 我想将此字符串粘贴到第 3 行和第 4 行之间:
我想知道我的环境中有什么类型的对象。 我可以像这样显示谁在那里: ls() 但是运行类似的东西 sapply(ls(), class) (显然)不会告诉我们我们拥有什么类型(类)的对象(函数、数字、因
我想创建一个带有水平标签的树状图,但让叶子根据它们的高度悬挂,而不是仅仅下降到图的边缘。 例子: par(mfrow = c(1,2)) hc <- hclust(dist(USArrests), "
我的 CSS 中有一个元素,如下所示 .xyz{ position:absolute; left:50%; } 现在正如预期的那样,当我减小浏览器窗口的宽度时,这个元素向左移动
我是一名优秀的程序员,十分优秀!