上次使用可移动对象时,编译器是否会自动使用移动语义? [英] Do compilers automatically use move semantics when a movable object is used for the last time?

查看:58
本文介绍了上次使用可移动对象时,编译器是否会自动使用移动语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究右值引用,并得出的结论是,在将要复制对象完整副本的任何地方使用按值传递是非常有利的(有关完整的证明,请参见例如想要的速度?按值传递!),因为编译器可以自动优化复制副本,例如f(std::move(a));,其中f被定义为void f(A a);.

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);.

无处不在传递值的一个负面影响是,即使在诸如以下这样的简单情况下,所有代码也都被std::move乱扔了:

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;
}

即使没有提示,编译器也不难意识到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.

问题:

  1. C ++ 0x标准是否允许此优化?

  1. 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?

推荐答案

C ++ 0x标准是否允许这种优化?

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?

您应该用打印语句装饰A(const A&)A(A&&)并运行您感兴趣的测试用例.如果这些用例是设计的一部分,请不要忘记测试左值参数.

You should decorate A(const A&) and A(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.

正确的答案取决于A的复制和移动成本,Object::value实际具有多少参数以及您愿意忍受多少代码重复.

The correct answers will depend upon how expensive the copy and move of A are,how many arguments Object::value actually has, and how much code repetition you're willing to put up with.

最后,要对包含总是"或无处不在"等字词的任何准则非常可疑.例如.我不时使用goto.但是其他程序员也有与goto关联的词,如从不".但是有时候,无论是速度还是清晰度,您都无法击败goto.

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 with goto. But every once in a while, you can't beat a goto for both speed and clarity.

有时候,您应该首选一对foo(const A&) foo(A&&),而不是foo(A).而且有时候你不会.您使用修饰后的复制和移动成员进行的实验将为您提供指导.

There will be times you should favor a pair of foo(const A&) foo(A&&) over foo(A). And times you won't. Your experiments with decorated copy and move members will guide you.

这篇关于上次使用可移动对象时,编译器是否会自动使用移动语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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