How does argument-dependent lookup work, and why does the using std::swap; swap(a, b); idiom matter?
Show answer and explanation
For an unqualified function call, argument-dependent lookup adds declarations from namespaces and classes associated with the argument types to ordinary lookup. A type can therefore provide a non-member swap in its own namespace, where it can efficiently exchange representation while preserving invariants. Generic code first writes using std::swap to make the standard fallback visible and then calls swap(a, b) unqualified so ADL can choose the more specialized overload. Calling std::swap directly suppresses that ADL customization, while adding arbitrary overloads to namespace std is not a general solution.
For an unqualified function call, argument-dependent lookup adds declarations from namespaces and classes associated with the argument types to ordinary lookup. A type can therefore provide a non-member swap in its own namespace, where it can efficiently exchange representation while preserving invariants. Generic code first writes using std::swap to make the standard fallback visible and then calls swap(a, b) unqualified so ADL can choose the more specialized overload. Calling std::swap directly suppresses that ADL customization, while adding arbitrary overloads to namespace std is not a general solution.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.