<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>

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

        Overloading global swap for user-defined type(重载用户定义类型的全局交换)

        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
                  }
                  

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

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

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

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  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>
                    • <small id='gV5qX'></small><noframes id='gV5qX'>

                    • <legend id='gV5qX'><style id='gV5qX'><dir id='gV5qX'><q id='gV5qX'></q></dir></style></legend>
                            <bdo id='gV5qX'></bdo><ul id='gV5qX'></ul>
                              <tbody id='gV5qX'></tbody>

                          • <tfoot id='gV5qX'></tfoot>