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

        • <bdo id='wZHd8'></bdo><ul id='wZHd8'></ul>
        <tfoot id='wZHd8'></tfoot>

      1. <legend id='wZHd8'><style id='wZHd8'><dir id='wZHd8'><q id='wZHd8'></q></dir></style></legend>
      2. <small id='wZHd8'></small><noframes id='wZHd8'>

      3. 如何为我的班级提供交换功能?

        时间:2024-05-12

            <tbody id='IoHA6'></tbody>

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

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

                  本文介绍了如何为我的班级提供交换功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 STL 算法中启用我的 swap 的正确方法是什么?

                  What is the proper way to enable my swap in STL algorithms?

                  1) 成员swap.std::swap 是否使用 SFINAE 技巧来使用成员 swap.

                  1) Member swap. Does std::swap use SFINAE trick to use the member swap.

                  2) 同一命名空间中的独立swap.

                  2) Free standing swap in the same namespace.

                  3) std::swap 的部分特化.

                  4) 以上所有.

                  谢谢.

                  看起来我没有清楚地表达我的问题.基本上,我有一个模板类,我需要 STL 算法来使用我为该类编写的(高效)交换方法.

                  Looks like I didn't word my question clearly. Basically, I have a template class and I need STL algos to use the (efficient) swap method I wrote for that class.

                  推荐答案

                  1. swap的正确使用.写library"的时候就这样写代码并希望在 swap 上启用 ADL(依赖于参数的查找).此外,这与 SFINAE 无关.
                  1. is the proper use of swap. Write it this way when you write "library" code and want to enable ADL (argument-dependent lookup) on swap. Also, this has nothing to do with SFINAE.

                  // some algorithm in your code
                  template<class T>
                  void foo(T& lhs, T& rhs) {
                      using std::swap; // enable 'std::swap' to be found
                                      // if no other 'swap' is found through ADL
                      // some code ...
                      swap(lhs, rhs); // unqualified call, uses ADL and finds a fitting 'swap'
                                      // or falls back on 'std::swap'
                      // more code ...
                  }
                  

                  1. 以下是为您的类提供 swap 函数的正确方法:
                  1. Here is the proper way to provide a swap function for your class:

                  namespace Foo {
                  
                  class Bar{}; // dummy
                  
                  void swap(Bar& lhs, Bar& rhs) {
                      // ...
                  }
                  
                  }
                  

                  如果 swap 现在使用,如 1) 所示,您的函数将被找到.此外,如果您绝对需要,您可以将该函数设为好友,或者提供由自由函数调用的成员 swap:

                  If swap is now used as shown in 1), your function will be found. Also, you may make that function a friend if you absolutely need to, or provide a member swap that is called by the free function:

                  // version 1
                  class Bar{
                  public:
                      friend void swap(Bar& lhs, Bar& rhs) {
                      // ....
                      }
                  };
                  
                  // version 2
                  class Bar{
                  public:
                      void swap(Bar& other) {
                      // ...
                      }
                  };
                  
                  void swap(Bar& lhs, Bar& rhs) {
                      lhs.swap(rhs);
                  }
                  
                  ...
                  

                  1. 您的意思是明确的专业化.Partial 仍然是别的东西,对于函数也不可能,只有结构/类.因此,由于您不能为模板类专门化 std::swap,您必须在您的命名空间中提供一个免费的函数.不是坏事,如果我可以这么说的话.现在,显式特化也是可能的,但通常您不想特化函数模板:
                  1. You mean an explicit specialization. Partial is still something else and also not possible for functions, only structs / classes. As such, since you can't specialize std::swap for template classes, you have to provide a free function in your namespace. Not a bad thing, if I may say so. Now, an explicit specialization is also possible, but generally you do not want to specialize a function template:

                  namespace std
                  {  // only allowed to extend namespace std with specializations
                  
                  template<> // specialization
                  void swap<Bar>(Bar& lhs, Bar& rhs) noexcept {
                      // ...
                  }
                  
                  }
                  

                  1. 不,因为 1) 不同于 2) 和 3).此外,同时拥有 2) 和 3) 将导致始终选择 2),因为它更适合.

                  这篇关于如何为我的班级提供交换功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将向量附加到自身的好方法 下一篇:擦除和删除的区别

                  相关文章

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

                  <legend id='gF5Dg'><style id='gF5Dg'><dir id='gF5Dg'><q id='gF5Dg'></q></dir></style></legend>

                  <tfoot id='gF5Dg'></tfoot>

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

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