- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
是否可以编写 C++ 代码,在可能的情况下依赖返回值优化 (RVO),但在不行的情况下回退到移动语义?例如,下面的代码由于有条件不能使用RVO,所以它把结果复制回来:
#include <iostream>
struct Foo {
Foo() {
std::cout << "constructor" << std::endl;
}
Foo(Foo && x) {
std::cout << "move" << std::endl;
}
Foo(Foo const & x) {
std::cout << "copy" << std::endl;
}
~Foo() {
std::cout << "destructor" << std::endl;
}
};
Foo f(bool b) {
Foo x;
Foo y;
return b ? x : y;
}
int main() {
Foo x(f(true));
std::cout << "fin" << std::endl;
}
这产生
constructor
constructor
copy
destructor
destructor
fin
destructor
这是有道理的。现在,我可以通过更改行来强制在上面的代码中调用移动构造函数
return b ? x : y;
到
return std::move(b ? x : y);
这给出了输出
constructor
constructor
move
destructor
destructor
fin
destructor
但是,我不太喜欢直接调用 std::move。
实际上,问题是我处于这样一种情况,即使构造函数存在,我也绝对不能调用复制构造函数。在我的用例中,要复制的内存太多,虽然删除复制构造函数会很好,但出于各种原因,这不是一个选项。同时,我想从函数中返回这些对象并且更愿意使用 RVO。现在,我真的不想在编码时、应用时和不应用时记住 RVO 的所有细微差别。大多数情况下,我希望返回对象而不希望调用复制构造函数。当然,RVO 更好,但移动语义很好。有没有办法在可能的情况下使用 RVO,在没有的情况下使用移动语义?
以下question帮助我弄清楚发生了什么。基本上,标准的 12.8.32 规定:
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 ]
好吧,为了弄清楚复制 elison 的标准是什么,我们看看 12.8.31
in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cvunqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
因此,如果我们将 f 的代码定义为:
Foo f(bool b) {
Foo x;
Foo y;
if(b) return x;
return y;
}
然后,我们的每个返回值都是一个自动对象,因此 12.8.31 表示它符合复制 elison 的条件。这会转到 12.8.32,它表示复制的执行就像它是一个右值一样。现在,RVO 不会发生,因为我们事先不知道要采用哪条路径,但是由于 12.8.32 中的要求,移动构造函数被调用。从技术上讲,复制到 x 时避免了一个移动构造函数。基本上,在运行时,我们得到:
constructor
constructor
move
destructor
destructor
fin
destructor
在构造函数上关闭 elide 生成:
constructor
constructor
move
destructor
destructor
move
destructor
fin
destructor
现在,假设我们回到
Foo f(bool b) {
Foo x;
Foo y;
return b ? x : y;
}
我们必须看看 5.16.4 中条件运算符的语义
If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
因为 x 和 y 都是左值,所以条件运算符是左值,但不是自动对象。因此,12.8.32 不会启动,我们将返回值视为左值而不是右值。这需要调用复制构造函数。因此,我们得到
constructor
constructor
copy
destructor
destructor
fin
destructor
现在,由于本例中的条件运算符基本上是复制值类别,这意味着代码
Foo f(bool b) {
return b ? Foo() : Foo();
}
将返回一个右值,因为条件运算符的两个分支都是右值。我们看到这一点:
constructor
fin
destructor
如果我们关闭构造函数的省略,我们会看到移动
constructor
move
destructor
move
destructor
fin
destructor
基本上,我们的想法是,如果我们返回一个右值,我们将调用移动构造函数。如果我们返回一个左值,我们将调用复制构造函数。当我们返回一个类型与返回类型匹配的非 volatile 自动对象时,我们返回一个右值。如果我们有一个像样的编译器,这些拷贝和移动可能会被 RVO 省略。但是,至少,我们知道在无法应用 RVO 的情况下调用什么构造函数。
最佳答案
当 return 语句中的表达式是一个非 volatile 自动持续时间对象,而不是函数或 catch 子句参数,具有与函数返回类型相同的 cv-unqualified 类型时,生成的复制/移动符合条件复制省略。该标准还继续说,如果禁止复制省略的唯一原因是源对象是函数参数,并且如果编译器无法省略拷贝,则拷贝的重载决议应该像expression 是一个右值。因此,它更喜欢移动构造函数。
OTOH,因为您使用的是三元表达式,所以所有条件都不成立,您只能使用常规拷贝。将您的代码更改为
if(b)
return x;
return y;
调用移动构造函数。
请注意,RVO 和复制省略之间存在区别 - 复制省略是标准允许的,而 RVO 是一种常用于在标准允许复制省略的情况下删除拷贝的技术。
关于c++ - 我们能否在可能的情况下使用返回值优化,而在可能的情况下退回到移动而不是复制语义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633297/
我正在尝试提升我的 javascript 编程技能(或者说我的编程技能时期 :)) 所以我试图理解一些语义: 第一行的“?”是什么意思?均值和“-distance”中的减号 第二行中的“+=”或“-=
我正在尝试在语义 UI 中执行复选框,但它不起作用,我无法弄清楚我做错了什么。 我包括jquery、semantic.min.js、checkbox.js 和semantic.min.css,然后我添
我正在构建一个 Spring 后端。我有一个 Controller ,它获取一个“搜索对象” - 一个具有 10 个字段的对象,其中只有一个应该被填充,所以搜索功能(我没有编写,但需要对其进行更改和重
我面临着编写更智能/高级的“相关内容”算法的挑战,并且不知道从哪里开始,所以我决定提出一个问题,是否有人会指出我正确的方向。 我们的数据库包含很多文章,到目前为止,我们使用关键字/标签查询了相关文章,
我正在尝试将通用字符串写入Rust中的数字函数,其中支持的类型为i16,i32,i64,u32,u64,f32和f64。 最初我有这个: fn str_to_num(s: &str, default_
假设我们在 hpp 文件中有一个带有唯一指针的简单结构: struct SomeType { SomeType() = default; ~SomeType(); st
这是同一预处理指令的多个问题。 1 - <> 还是 ""? 除了在 MSDN 中找到的信息: #include Directive (C-C++) 1.a:这两种符号有什么区别? 1.b:所有编译器都
所以基本上我有一个带有列表的简单系统,当我选择一个项目时,它会显示描述和绑定(bind)到该项目的图像。 项目:https://jsfiddle.net/jhnjcddh/2/ 问题是我需要在 JS
很抱歉问了一个愚蠢的问题,但有人能告诉我以下是什么意思吗 for ctype, (codename, name) in searched_perms: 我不明白括号里是怎么回事。 for ctype
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why do all these crazy function pointer definitions al
我正在学习 HTML5,并获得了一个将 CSS Zen Gardens 转换为 HTML5 语义版本的项目。我已经能够轻松地转换其中的大部分内容,但是底部的链接/导航给我带来了一些问题。 转换此/处理
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improv
我一直在我的 emacs c/c++ 开发设置中试验 cedet 和语义,除了一个小细节外,我对它非常满意。 我使用 ede-cpp-root-project 创建一个项目,并给出我的项目的根目录以及
引用问题自http://www.garfieldtech.com/blog/put-up-with-put (这是针对 Drupal 开源项目的,有点元,因为这里没有代码): GET、HEAD 和 P
我有以下代码。 let v_blue = UIView() v_blue.backgroundColor = UIColor.blueColor() l
我目前正在 objc.io 上阅读优秀的 Advanced Swift 书籍,但遇到了一些我不明白的问题。 如果您在操场上运行以下代码,您会注意到在修改字典中包含的结构时,下标访问会生成一个副本,但随
谁能给我一个关于 Flutter 上下文中语义概念的清晰解释(或链接)(它实际上是什么,何时使用,更新...)? 我在谷歌上搜索了很多,但到目前为止还没有找到任何好的解释。 非常感谢, 最佳答案 Di
这是我的代码 Was this what you wanted? It's good to see you again.
我有一个侧边栏,其中包含应用程序的主导航。它还包含一个 button 触发侧边栏的打开/关闭。在语义方面,标记应该是什么样的? 我应该把侧边栏放在一边,然后只在周围设置导航吗主导航,不包括打开/关闭触
考虑下面这行 Lisp 代码: (some-function 7 8 | 9) ;; some comment. note the extra indentation 该点位于“8”和
我是一名优秀的程序员,十分优秀!