• <bdo id='jxFZN'></bdo><ul id='jxFZN'></ul>
    <legend id='jxFZN'><style id='jxFZN'><dir id='jxFZN'><q id='jxFZN'></q></dir></style></legend>
  1. <i id='jxFZN'><tr id='jxFZN'><dt id='jxFZN'><q id='jxFZN'><span id='jxFZN'><b id='jxFZN'><form id='jxFZN'><ins id='jxFZN'></ins><ul id='jxFZN'></ul><sub id='jxFZN'></sub></form><legend id='jxFZN'></legend><bdo id='jxFZN'><pre id='jxFZN'><center id='jxFZN'></center></pre></bdo></b><th id='jxFZN'></th></span></q></dt></tr></i><div id='jxFZN'><tfoot id='jxFZN'></tfoot><dl id='jxFZN'><fieldset id='jxFZN'></fieldset></dl></div>
    1. <small id='jxFZN'></small><noframes id='jxFZN'>

    2. <tfoot id='jxFZN'></tfoot>

      我应该使用 std::string 的右值编写构造函数吗?

      时间:2023-09-26

      <i id='Z2myK'><tr id='Z2myK'><dt id='Z2myK'><q id='Z2myK'><span id='Z2myK'><b id='Z2myK'><form id='Z2myK'><ins id='Z2myK'></ins><ul id='Z2myK'></ul><sub id='Z2myK'></sub></form><legend id='Z2myK'></legend><bdo id='Z2myK'><pre id='Z2myK'><center id='Z2myK'></center></pre></bdo></b><th id='Z2myK'></th></span></q></dt></tr></i><div id='Z2myK'><tfoot id='Z2myK'></tfoot><dl id='Z2myK'><fieldset id='Z2myK'></fieldset></dl></div>
      <legend id='Z2myK'><style id='Z2myK'><dir id='Z2myK'><q id='Z2myK'></q></dir></style></legend>
      • <bdo id='Z2myK'></bdo><ul id='Z2myK'></ul>
          <tbody id='Z2myK'></tbody>
        1. <tfoot id='Z2myK'></tfoot>

          <small id='Z2myK'></small><noframes id='Z2myK'>

              • 本文介绍了我应该使用 std::string 的右值编写构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个简单的类:

                class X
                {
                    std::string S;
                    X (const std::string& s) : S(s) { }
                };
                

                我最近阅读了一些关于右值的文章,我一直在想,是否应该使用右值为 X 编写构造函数,这样我就可以检测 的临时对象std::string 类型?

                I've read a bit about rvalues lately, and I've been wondering, if I should write constructor for X using rvalue, so I would be able do detect temporary objects of std::string type?

                我认为它应该是这样的:

                I think it should look something like:

                X (std::string&& s) : S(s) { }
                

                据我所知,在支持 C++11 的编译器中实现 std::string 应该在可用时使用它的移动构造函数.

                As to my knowledge, implementation of std::string in compilers supporting C++11 should use it's move constructor when available.

                推荐答案

                X (std::string&& s) : S(s) { }
                

                这不是一个带有 rvalue 的构造函数,而是一个带有 rvalue-reference 的构造函数.在这种情况下,您不应该使用 rvalue-references.而是通过值传递然后移动到成员中:

                That is not a constructor taking an rvalue, but a constructor taking an rvalue-reference. You should not take rvalue-references in this case. Rather pass by value and then move into the member:

                X (std::string s) : S(std::move(s)) { }
                

                经验法则是,如果您需要复制,请在界面中进行.

                The rule of thumb is that if you need to copy, do it in the interface.

                这篇关于我应该使用 std::string 的右值编写构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:我应该总是继续使用 `sink` 构造函数或 setter 参数吗? 下一篇:与硬件接口的 C++ 构造函数应该真正起作用吗?

                相关文章

              • <i id='DK4sJ'><tr id='DK4sJ'><dt id='DK4sJ'><q id='DK4sJ'><span id='DK4sJ'><b id='DK4sJ'><form id='DK4sJ'><ins id='DK4sJ'></ins><ul id='DK4sJ'></ul><sub id='DK4sJ'></sub></form><legend id='DK4sJ'></legend><bdo id='DK4sJ'><pre id='DK4sJ'><center id='DK4sJ'></center></pre></bdo></b><th id='DK4sJ'></th></span></q></dt></tr></i><div id='DK4sJ'><tfoot id='DK4sJ'></tfoot><dl id='DK4sJ'><fieldset id='DK4sJ'></fieldset></dl></div>

                    <tfoot id='DK4sJ'></tfoot>
                  1. <small id='DK4sJ'></small><noframes id='DK4sJ'>

                  2. <legend id='DK4sJ'><style id='DK4sJ'><dir id='DK4sJ'><q id='DK4sJ'></q></dir></style></legend>
                      <bdo id='DK4sJ'></bdo><ul id='DK4sJ'></ul>