C++ Logo

std-discussion

Advanced search

Re: Member function, using, and ADL

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Sat, 29 May 2021 17:14:42 +0100
On Sat, 29 May 2021 at 15:36, Yongwei Wu via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> I happened to find Alan Griffiths' article ‘C++ Standards - The "swap"
> Problem"’ (<URL:https://accu.org/journals/overload/9/41/griffiths_466/>),
> and I did some tests while reading it. I was happy and surprised to find
> what he described seemed a non-issue now.
>

Alan appears (well - that article is over 20 years old, so "appeared" would
be more accurate) to have been under the misconception that "using
std::swap" would cause the immediately following call to the unqualified
name "swap" to resolve to "std::swap" and not to "arg::swap" (for some
ADL-associated namespace "arg"). In fact, even in C++98 (the extant
Standard at the time) the relevant passage is clear:

> If the ordinary unqualified lookup of the name finds the declaration of a
class member function, the associated
namespaces and classes are not considered. Otherwise the set of
declarations found by the lookup of
the function name is the union of the set of declarations found using
ordinary unqualified lookup and the set
of declarations found in the namespaces and classes associated with the
argument types.

That is, the point of "using std::swap;" is twofold; it both hides any
class member "swap", and makes "std::swap" available as a fallback if ADL
does not find a suitable candidate. But ADL-located candidate names are
preferred.

I don't know how that misconception made it into print; quite possibly at
the time compilers did not implement Standard C++ correctly. (MSVC was
certainly incorrect at the time). But this is is something that has always
worked according to the letter of the Standard.

Received on 2021-05-29 11:14:57