<legend id='8SFhG'><style id='8SFhG'><dir id='8SFhG'><q id='8SFhG'></q></dir></style></legend>
    • <bdo id='8SFhG'></bdo><ul id='8SFhG'></ul>

    <small id='8SFhG'></small><noframes id='8SFhG'>

    1. <tfoot id='8SFhG'></tfoot>

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

        重载用户定义类型的全局交换

        时间:2024-08-14

        1. <small id='lI4nm'></small><noframes id='lI4nm'>

            <tbody id='lI4nm'></tbody>

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

              <tfoot id='lI4nm'></tfoot>
                <bdo id='lI4nm'></bdo><ul id='lI4nm'></ul>
                  本文介绍了重载用户定义类型的全局交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  C++ 标准禁止在名称空间 std 中声明类型或定义任何内容,但它允许您为用户定义的类型专门化标准 STL 模板.

                  The C++ standard prohibits declaring types or defining anything in namespace std, but it does allow you to specialize standard STL templates for user-defined types.

                  通常,当我想为我自己的自定义模板类型专门化 std::swap 时,我只是这样做:

                  Usually, when I want to specialize std::swap for my own custom templated type, I just do:

                  namespace std
                  {
                    template <class T>
                    void swap(MyType<T>& t1, MyType<T>& t2)
                    {
                       t1.swap(t2);
                    }
                  }
                  

                  ...而且效果很好.但我不完全确定我的通常做法是否符合标准.我这样做正确吗?

                  ...and that works out fine. But I'm not entirely sure if my usual practice is standard compliant. Am I doing this correctly?

                  推荐答案

                  你所拥有的不是专业化,它是重载,正是标准所禁止的.(但是,它目前在实践中几乎总是有效,并且您可能可以接受.)

                  What you have is not a specialization, it is overloading and exactly what the standard prohibits. (However, it will almost always currently work in practice, and may be acceptable to you.)

                  以下是您为类模板提供自己的交换的方法:

                  Here is how you provide your own swap for your class template:

                  template<class T>
                  struct Ex {
                    friend void swap(Ex& a, Ex& b) {
                      using std::swap;
                      swap(a.n, b.n);
                    }
                    T n;
                  }
                  

                  这里是你如何调用 swap,你会注意到 Ex 的 swap 中也使用了它:

                  And here is how you call swap, which you'll notice is used in Ex's swap too:

                  void f() {
                    using std::swap; // std::swap is the default or fallback
                    Ex<int> a, b;
                    swap(a, b); // invokes ADL
                  }
                  

                  相关:函数模板专业化重要性和必要性

                  这篇关于重载用户定义类型的全局交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::next_permutation 的摊销复杂度? 下一篇:在移动赋值和移动构造函数方面实现 std::swap

                  相关文章

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

                  3. <legend id='gV5qX'><style id='gV5qX'><dir id='gV5qX'><q id='gV5qX'></q></dir></style></legend>
                        <bdo id='gV5qX'></bdo><ul id='gV5qX'></ul>

                    1. <tfoot id='gV5qX'></tfoot>