当最后一次使用可移动对象时,编译器是否会自动使用移动语义?
我最近一直在研究右值引用,并得出结论,在将创建对象的完整副本的任何地方使用按值传递是非常有利的(有关完整的理由,请参见例如 添加右值引用运算符重载时如何减少冗余代码?和想要速度?按值传递!),因为编译器可以自动优化副本在诸如 f(std::move(a));
的情况下,其中 f
定义为 void f(A a);
。
无处不在的值传递的一个负面后果是,即使在简单的情况下,所有代码都会充斥着 std::move
,例如:
void Object::value(A a)
{
value_ = std::move(a);
}
显然,如果我只写了以下内容:
void Object::value(A a)
{
value_ = a;
}
它不应该即使没有提示,编译器也很难认识到 a
已接近其生命周期的终点,并且不会用额外的副本来惩罚我。事实上,即使在复杂的函数中,编译器也应该能够识别这一点。
问题:
C++0x 标准允许这种优化吗?
编译器使用它吗?即使在复杂的情况下,即函数由多行组成?
此优化的可靠性如何,即我可以期望编译器像我期望编译器应用返回值优化一样使用它吗?
I've been studying rvalue references lately and came to a conclusion that it's quite advantageous to use pass-by-value everywhere where complete copy of an object will be made (for complete justification see e.g. How to reduce redundant code when adding rvalue reference operator overloads? and Want speed? Pass by value!), because the compiler can automatically optimize a copy away in cases such as f(std::move(a));
, where f
is defined as void f(A a);
.
One negative consequence of pass-by-value-everywhere is that all the code becomes littered with std::move
even in simple cases such as:
void Object::value(A a)
{
value_ = std::move(a);
}
Obviously, if I wrote only the following:
void Object::value(A a)
{
value_ = a;
}
it shouldn't be hard for the compiler to recognize that a
is near the end of its lifetime even without the hint and not to penalize me with additional copy. In fact, the compiler should be able to recognize this even in complex functions.
The questions:
Is this optimization allowed by the C++0x Standard?
Do the compilers employ it? Even in complex cases, i.e. the function consists from more than one line?
How reliable is this optimization, i.e. can I expect the compiler to utilize it as much as I expect the compiler to apply Return Value Optimization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。
不。
您应该使用 print 语句装饰
A(const A&)
和A(A&&)
并运行您感兴趣的测试用例。如果这些用例是您设计的一部分,请不要忘记测试左值参数。正确的答案取决于
A
的复制和移动的成本、Object::value
实际上有多少个参数,以及您愿意重复多少代码忍受。最后,对任何包含“总是”或“无处不在”等词语的指导方针要非常怀疑。例如,我偶尔会使用
goto
。但其他程序员会将“never”之类的词与goto
相关联。但时不时地,您无法在速度和清晰度方面击败goto
。有时您应该更喜欢一对
foo(const A&) foo(A&&)
而不是foo(A)
。有时你不会。您对装饰复制和移动成员的实验将为您提供指导。No.
No.
You should decorate
A(const A&)
andA(A&&)
with print statements and run test cases of interest to you. Don't forget to test lvalue arguments if those use cases are part of your design.The correct answers will depend upon how expensive the copy and move of
A
are,how many argumentsObject::value
actually has, and how much code repetition you're willing to put up with.Finally, be very suspicious of any guideline that contains words like "always" or "everywhere". E.g. I use
goto
every once in a while. But other programmers have words like "never" associated withgoto
. But every once in a while, you can't beat agoto
for both speed and clarity.There will be times you should favor a pair of
foo(const A&) foo(A&&)
overfoo(A)
. And times you won't. Your experiments with decorated copy and move members will guide you.