Consider for this question the following code:
#include
namespace ns
{
struct foo
{
foo() : i(0) {}
In C++20, this is finally standardized:
std::swap(a, b);
This uses ADL to call the correct overload and imposes the correct requirements to use in SFINAE. The magic is specified in [namespace.std]/7:
Other than in namespace
stdor in a namespace within namespacestd, a program may provide an overload for any library function template designated as a customization point, provided that (a) the overload's declaration depends on at least one user-defined type and (b) the overload meets the standard library requirements for the customization point.174 [ Note: This permits a (qualified or unqualified) call to the customization point to invoke the most appropriate overload for the given arguments. — end note ]174) Any library customization point must be prepared to work adequately with any user-defined overload that meets the minimum requirements of this document. Therefore an implementation may elect, under the as-if rule ([intro.execution]), to provide any customization point in the form of an instantiated function object ([function.objects]) even though the customization point's specification is in the form of a function template. The template parameters of each such function object and the function parameters and return type of the object's
operator()must match those of the corresponding customization point's specification.
(emphasis mine)
And swap is designated as a customization point in [utility.swap]:
templateconstexpr void swap(T& a, T& b) noexcept(see below); Remarks: This function is a designated customization point ([namespace.std]) and shall not participate in overload resolution unless
is_move_constructible_vistrueandis_move_assignable_vistrue. The expression insidenoexceptis equivalent to:is_nothrow_move_constructible_v&& is_nothrow_move_assignable_v Requires: Type
Tshall be Cpp17MoveConstructible (Table 26) and Cpp17MoveAssignable (Table 28).Effects: Exchanges values stored in two locations.
(emphasis mine)