Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you overload swap in the std namespace?

Tags:

c++

stl

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::swap;
swap(something, soemthingelse);

The reason for this is to use argument dependent look-up to either use a swap function in a user namespace or swap in the std namespace. This raised an interested question for me. When I overload std::swap for one of my classes I had actually been defining it in the std namespace... namespace std { void swap(/*...*/){/*...*/} }. Is this practice wrong? Should I define my own swaps in std or my own namespace (and why)?

like image 716
David Avatar asked Jan 18 '13 16:01

David


2 Answers

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.

like image 96
Jonathan Wakely Avatar answered Nov 11 '22 10:11

Jonathan Wakely


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.

like image 6
David Rodríguez - dribeas Avatar answered Nov 11 '22 09:11

David Rodríguez - dribeas