我是唯一一个觉得 std::move 有点难以理解的人吗?

发布于 2024-10-02 09:14:54 字数 127 浏览 6 评论 0原文

所以我一直在 SO 和其他地方阅读有关 std::move 、 std::forward 、右值、左值广告等的内容。但我发现我抓不住。尽管我有时会进行修复,但我认为我了解 C++ 中关于指针、引用等的基本内容。是我的问题还是这些东西太重了?

So I have been reading about std::move, std::forward, rvalues, lvalues ad so on in SO and other places. But I find that I can't grasp it. Even though I sometimes get into fixes, I think I understand basic stuff about pointers, references, etc which were in C++ before all this. Is it me or are these stuff getting too heavy?

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

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

发布评论

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

评论(2

鹿! 2024-10-09 09:14:54

如果您还没有阅读原始提案,我建议您阅读:

添加移动语义支持的提案C++语言

它非常清楚地列出了可以使用右值引用和移动语义解决的问题,以及如何使用右值引用和移动语义来解决这些问题。

标准委员会的文件通常晦涩难懂,但这篇文章很容易理解,非常值得一读。最终 C++0x 标准中指定的右值引用和移动语义(无论何时发生)可能与本文中提出的不同,但概念仍然相同。

I would recommend reading the original proposal if you haven't already:

A Proposal to Add Move Semantics Support to the C++ Language

It lays out very clearly the problems that can be solved with rvalue references and move semantics and how rvalue references and move semantics can be used to solve those problems.

Standards committee papers are often dense and difficult to understand, but this one is quite accessible and very much worth reading. The rvalue references and move semantics as specified in the final C++0x standard (whenever that happens) may be different from what is proposed in this paper, but the concepts are still the same.

临走之时 2024-10-09 09:14:54

你的问题很笼统。也许我可以帮助您开始:

  • 忽略开头的函数 std:move()std::forward()
  • RValue 引用的一个方面 是保存临时的。
    • 在 C++03 代码中,计算 Matrix z = a + b + c + d; 的临时数(使用 Matrix a,b,c,d;
    • 使用重载的RValue引用Matrix上自行实现operator+
    • 您应该能够大大减少临时对象的数量。

如果您想查看 std::move() 的简单用法:帮助编译器避免引入返回值的副本:

  • 创建一个像 Image 这样的容器类 - - 复制成本高昂。
  • 不要忘记实现移动复制分配
  • 发明一个像这样工作的工厂函数:

    图像 load_matching_size(const char *fn_small, const char *fn_big) {
       对<图像> ii = load_2_images(fn_small, fn_big);
       返回 ii.first.width() >= 64 ? ii.第一:ii.第二;
    }
    
  • 你能数一下临时变量的数量吗?请注意,退货将需要额外的一份和副本! (该示例的设计使得返回值优化(“RVO”)不应该成为可能)

  • 您能看出这不是必要的吗?函数返回后不久,ii 中的图像将被丢弃。编译器可以使用 then 作为返回值吗? (不,它不能。如果我们只有一个 Image,RVO 就可以工作)。
  • 通过 return 中的 move,您可以告诉编译器您不再需要 ii 并且它可以将其用于返回。因此,我可以使用 move-c'tor 而不是 copy-c'tor 进行退货,从而节省昂贵的副本。

Your question is very general. Maybe I can get you started:

  • Ignore the functions std:move() and std::forward() at the beginning
  • One aspect of RValue References is to save temporaries.
    • In C++03 code, count the temps for Matrix z = a + b + c + d; (with Matrix a,b,c,d;)
    • implement yourself operator+ on Matrix with overloaded RValue References.
    • you should be able to reduce the number of temporaries greatly.

If you want to see a simple use of std::move(): Help the compiler to avoid introducing a copy for a return value:

  • Make a container class like Image -- costly to copy.
  • Do not forget to implement the move and copy-assign
  • invent a factory function that works like this:

    Image load_matching_size(const char *fn_small, const char *fn_big) {
       pair<Image> ii = load_2_images(fn_small, fn_big);
       return ii.first.width() >= 64 ? ii.first : ii.second;
    }
    
  • Can you count the numbers of temporaries? Note that the return will need an additional one and copy! (The example is designed so that return-value-optimization ("RVO") should not be possible)

  • Can you see that this would not be necessary? The Images in ii will be thrown away shorty after the function returned. Could the compiler use then for the return value? (No it could not. RVO would work if we had only one Image).
  • With move in the return you can tell the compiler, that you do not need ii further and that it can use it for the return. And thus spare a costly copy my using the move-c'tor instead of the copy-c'tor for the return.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文