当最后一次使用可移动对象时,编译器是否会自动使用移动语义?

发布于 2024-11-09 12:16:56 字数 756 浏览 3 评论 0原文

我最近一直在研究右值引用,并得出结论,在将创建对象的完整副本的任何地方使用按值传递是非常有利的(有关完整的理由,请参见例如 添加右值引用运算符重载时如何减少冗余代码?想要速度?按值传递!),因为编译器可以自动优化副本在诸如 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 已接近其生命周期的终点,并且不会用额外的副本来惩罚我。事实上,即使在复杂的函数中,编译器也应该能够识别这一点。

问题:

  1. C++0x 标准允许这种优化吗?

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

  3. 此优化的可靠性如何,即我可以期望编译器像我期望编译器应用返回值优化一样使用它吗?

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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[浮城] 2024-11-16 12:16:56

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

不。

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

不。

此优化的可靠性如何,
即我可以期望编译器
尽可能多地利用它
编译器应用返回值
优化?

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

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

最后,对任何包含“总是”或“无处不在”等词语的指导方针要非常怀疑。例如,我偶尔会使用 goto 。但其他程序员会将“never”之类的词与goto 相关联。但时不时地,您无法在速度和清晰度方面击败 goto

有时您应该更喜欢一对 foo(const A&) foo(A&&) 而不是 foo(A)。有时你不会。您对装饰复制和移动成员的实验将为您提供指导。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文