当最后一次使用可移动对象时,编译器自动使用移动语义 [英] do compilers automatically use move semantics when a movable object is used for the last time

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

问题描述

我最近一直在研究右值引用,得出的结论是,在任何完成对象完整副本的地方使用传递值是非常有利的(完整的论证参见在这里这里),因为编译器可以自动优化副本,例如 f(std :: move(a)); ,其中 f 定义为 void



pass-by-value-无处不在的一个负面结果是,所有的变量都变成了散布着 std :: move 即使在简单的情况下,例如:

  void Object :: value(A a)
{
value_ = std :: move(a);
}

显然,如果我只写了以下内容:

  void Object :: value(A a)
{
value_ = a;
}

编译器不应该难以识别 a 接近它的生命的尽头,即使没有提示,不惩罚我与额外的副本。事实上,编译器应该能够在复杂的函数中识别这一点。



问题:



)这是C ++ 0x标准允许的优化吗?



2)编译器使用它吗?即使在复杂的情况下,即函数由多行组成?



3)这种优化的可靠性,即我可以期望编译器利用它我希望编译器应用返回值优化?

解决方案


+ 0x标准?


否。


编译器是否使用它?即使在
复杂情况下,即函数
由多行组成吗?


p>


这个优化的可靠性,
ie我可以期望编译器
使用它,正如我预期的
编译器应用返回值
优化?


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



正确的答案取决于复制和移动 A 是,有多少参数 Object :: value 实际上有,以及你愿意忍受多少代码重复。 / p>

最后,对于包含always或everywhere等字词的任何指南,非常可疑。例如。我每隔一段时间使用 goto 。但是其他程序员有类似never与 goto 相关联的词。但是,每隔一段时间,你不能为了速度和清晰度而击败 goto



是你应该赞成 foo(const A&)foo(A&& amp;) over foo(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. here and here), 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 come 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:

1) Is this optimization allowed by the C++0x Standard?

2) Do the compilers employ it? Even in complex cases, i.e. the function consists from more than one line?

3) 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?

解决方案

Is this optimization allowed by the C++0x Standard?

No.

Do the compilers employ it? Even in complex cases, i.e. the function consists from more than one line?

No.

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?

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.

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.

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.

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天全站免登陆