Should you overload swap in the std namespace?

前端 未结 5 1983
闹比i
闹比i 2020-12-06 00:22

I read something interesting today that said the \'standard\' way to call swap on a user provided type (provided as a template argument) is...

using std::swa         


        
相关标签:
5条回答
  • 2020-12-06 00:42

    First of all, you are not allowed to add things to std (be it just an overload), so overloading the std::swap is bad practice in the first place. What you can do is specialize existing templates. So you should rather specialize than overload:

    namespace std
    {
        template<> void swap<MyType>(...) {...}
    }
    

    But still the question stands if an own-namespace version is to be preferred over a std-specialization. The idomatic recommended way is to provide a free swap in your own namespace and let ADL resolve it, like David already suggests. The problem with this is, that if someone (the client of your library) is not that well-versed in idiomatic C++ and just explicitly calls std::swap(...) all over the place, you might end up with sub-optimal swapping behaviour. This is the case why I for myself usually went the safe way of doing both (own-namespace function + std-specialization), even if that always has a bad taste to me.

    But fortunately C++11 simplifies things, since efficiently moveable types (which are usually also the effieciently swappable ones) can be swapped quite efficiently with the default std::swap, which uses move-semantics. So the possible usage of the default std::swap over your own swap function becomes less of a disadvantage (and you might even consider not bothering to provide one anyway).

    So if you have C++11, the best way is indeed to not bother extending std but just define your own swap in your types' namespace, while in C++03 you might want to be on the safe side with a std::swap specialization, even if being less idiomatic.

    0 讨论(0)
  • 2020-12-06 00:44

    You're doing it wrong :)

    17.6.2.4.1 [namespace.std]

    1. The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

    That pretty clearly says you may not add overloads to namespace std. You could specialize std::swap<MyType> for your type, but if your type is a template you'd need a partial specialization, std::swap<MyContainer<T>> and you can't partially specialize a function template, so that won't work, so it's not a good approach in general.

    C++11 also defines the requirements for a type to be swappable which include:

    17.6.3.2 [swappable.requirements]

    1. ...
    2. ...
    3. The context in which swap(t, u) and swap(u, t) are evaluated shall ensure that a binary non-member function named "swap" is selected via overload resolution (13.3) on a candidate set that includes:
    • the two swap function templates defined in <utility> (20.2) and
    • the lookup set produced by argument-dependent lookup (3.4.2).

    So calling swap on two objects of swappable type should be able to find std::swap and should be able to find other overloads by ADL. Calling it unqualified (and without an explicit template argument list) ensures ADL happens, and including <utility> and adding the using-declaration for std::swap ensures the standard overloads can be found. So doing it the way you show in your question meets those requirements.

    That pretty clearly defines what it takes to be swappable in the sense used by the standard, which is what is required by the standard library e.g. by the functions in <algorithm>.

    If you put swap overloads for your type in your type's namespace then they can be found by ADL. That is the right thing to do anyway, functions related to your type belong in the same namespace as your type, see Item 57 in C++ Coding Standards by Sutter and Alexandrescu for more details on that topic.

    So in short, you have been doing it wrong. What you read is correct. Doing using std::swap and relying on ADL always works (for templates and non-templates) and avoids undefined behaviour. Yay.

    N.B. The C++03 standard was less clear about how user-defined types should be swapped. For some history around this area see N1691 2.2 which defines the term customization point and shows different ways to define them in APIs. The protocol used in C++11 for swapping types uses one of those ways, and is now clearly and unambiguously blessed as the "correct way" to provide a swap function for your type. Other customization points in other libraries can use other approaches, but to be swappable in C++11 terms means using std::swap; and relying on ADL.

    0 讨论(0)
  • 2020-12-06 00:50

    Ideal thing to do is to provide a public swap member function only when you think that the std::swap would be inefficient for your type. In case you provide a swap member function, it is recommended that you also provide a non-member swap that calls this member function. This makes it easier for other people to call your more efficient template-specific version.

    0 讨论(0)
  • 2020-12-06 00:53

    I believe this is the answer you're looking for, and the entire set of answers to that question explain all. Howard Hinnant and Dave Abrahams have been on the C++ standards committee for more than a decade I think.

    0 讨论(0)
  • 2020-12-06 01:04

    It is legal to provide specializations of standard templates for your own types, and those must go inside the std namespace. So both approaches are legal C++.

    That being said, the recommended way is to provide the swap free function in the same namespace as your own type and have ADL pickup your overload from there.


    EDIT: After some of the comments I reread the question and noticed that it mentions overloads in the std namespace. It is illegal to provide overloads in the std namespace, only specializations are allowed.

    0 讨论(0)
提交回复
热议问题