是否可以编写 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/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po