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

  • <small id='o2M9L'></small><noframes id='o2M9L'>

      1. <legend id='o2M9L'><style id='o2M9L'><dir id='o2M9L'><q id='o2M9L'></q></dir></style></legend>

          <bdo id='o2M9L'></bdo><ul id='o2M9L'></ul>

        我应该总是继续使用 `sink` 构造函数或 setter 参数吗?

        Should I always move on `sink` constructor or setter arguments?(我应该总是继续使用 `sink` 构造函数或 setter 参数吗?)

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

              <small id='1eNKT'></small><noframes id='1eNKT'>

                <bdo id='1eNKT'></bdo><ul id='1eNKT'></ul>
                  本文介绍了我应该总是继续使用 `sink` 构造函数或 setter 参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  struct TestConstRef {
                      std::string str;
                      Test(const std::string& mStr) : str{mStr} { }
                  };
                  
                  struct TestMove {
                      std::string str;
                      Test(std::string mStr) : str{std::move(mStr)} { }
                  };
                  

                  在观看 GoingNative 2013 后,我明白 sink 参数应该始终按值传递并使用 std::move 移动.TestMove::ctor 是应用这个习语的正确方法吗?是否存在 TestConstRef::ctor 更好/更高效的情况?

                  After watching GoingNative 2013, I understood that sink arguments should always be passed by value and moved with std::move. Is TestMove::ctor the correct way of applying this idiom? Is there any case where TestConstRef::ctor is better/more efficient?

                  琐碎的 setter 呢?我应该使用以下习语还是传递 const std::string&?

                  What about trivial setters? Should I use the following idiom or pass a const std::string&?

                  struct TestSetter {
                      std::string str;
                      void setStr(std::string mStr) { str = std::move(str); }
                  };
                  

                  推荐答案

                  简单的答案是:是的.

                  原因也很简单,如果您按值存储,您可能需要移动(从临时)或复制(从左值).让我们看看在这两种情况下会发生什么,两种方式.

                  The reason is quite simple as well, if you store by value you might either need to move (from a temporary) or make a copy (from a l-value). Let us examine what happens in both situations, with both ways.

                  来自临时

                  • 如果您通过 const-ref 获取参数,则临时对象将绑定到 const-ref 并且无法再次移动,因此您最终会制作一个(无用的)副本.
                  • 如果您按值获取参数,则该值将从临时(移动)中初始化,然后您自己从该参数中移动,因此不会进行复制.

                  一个限制:一个没有高效移动构造函数的类(例如 std::array),因为那样你做了两个副本而不是一个.

                  One limitation: a class without an efficient move-constructor (such as std::array<T, N>) because then you did two copies instead of one.

                  从左值(或 const 临时,但谁会这样做...)

                  From a l-value (or const temporary, but who would do that...)

                  • 如果你通过 const-ref 获取参数,那里什么都不会发生,然后你复制它(不能从中移动),这样就得到了一个副本.
                  • 如果您按值获取参数,则将其复制到参数中,然后从参数中移出,从而生成单个副本.

                  一个限制:相同的...类,移动类似于复制.

                  One limitation: the same... classes for which moving is akin to copying.

                  因此,简单的答案是,在大多数情况下,通过使用接收器,您可以避免不必要的副本(通过移动来替换它们).

                  So, the simple answer is that in most cases, by using a sink you avoid unnecessary copies (replacing them by moves).

                  唯一的限制是移动构造函数与复制构造函数一样昂贵(或接近一样昂贵)的类;在这种情况下,有两个动作而不是一个副本是最糟糕的".幸运的是,这样的类很少见(数组是一种情况).

                  The single limitation is classes for which the move constructor is as expensive (or near as expensive) as the copy constructor; in which case having two moves instead of one copy is "worst". Thankfully, such classes are rare (arrays are one case).

                  这篇关于我应该总是继续使用 `sink` 构造函数或 setter 参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <legend id='4GoT8'><style id='4GoT8'><dir id='4GoT8'><q id='4GoT8'></q></dir></style></legend>

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

                            <small id='4GoT8'></small><noframes id='4GoT8'>