C++ Logo

std-proposals

Advanced search

Re: Operator functions to fix ADL

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 13 Oct 2020 10:34:10 -0400
On Mon, Oct 12, 2020 at 12:54 PM Григорий Шуренков via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> [Someone wrote:]
> > As one of the "I am just a regular user, not a compiler guru" lurkers on
> this list, I'd just like to mention that anything that involves call-site
> annotation is a great source of confusion for regular users, especially
> when the answer to "Why?" is "obscure reason X" as it would be in this
> case. Shifting the responsibility to the caller is just as ineffective as
> documenting the issue in a manual.
>
> Let me explain why it might be a good idea to have a call-site annotation
> for ADL. The reason is not obscure, the annotation designates an unusual
> function call - a customization point. Moreover, until recently it was a
> recommended practice to annotate such calls in the following way:
> using std::swap;
> swap(a, b);
>

I agree with you both. :) One of the big problems with ADL — and
unfortunately the biggest problem with ADL is that we can't get rid of it
;) — is that it's a tool that can be used in several different ways, for
several different purposes. Sometimes the programmer wants to say "This
function is intended for use with ADL"; sometimes the programmer wants to
say "This call-site intends to use ADL." Either intent can be valid, at
different places in the same program.

See my blog post "What is the std::swap two-step?"
https://quuxplusone.github.io/blog/2020/07/11/the-std-swap-two-step/#when-should-i-use-the-two-step
where I compare plain old ADL to a pure virtual function, and
ADL-with-the-two-step to a non-pure, yet virtual, function.

I'd be interested to see a compiler patch to warn whenever "ADL lookup
occurred, on a non-operator, and ADL found a best-matching candidate which
would *not* have been found by regular unqualified lookup." It couldn't be
used in production because approximately every C++ program depends on ADL
*somewhere*. But it could give us a sense of the magnitude of the issue.

Third point is that such annotation allows what is currently not possible.
> If you look up the definition of range-based for loop in the standard you'd
> see that it is impossible to write its implementation in plain C++. To find
> begin and end for a container it performs only ADL w/o usual name lookup.
> An annotated call would do exactly that.
>

IIRC, std::ranges::begin(x) and the other Ranges CPOs are also specified to
perform only ADL without the usual unqualified name lookup. They have a way
to approximate that, by using a detail namespace and a "poison pill"; but
it's not 100% perfect and of course it's more expensive in terms of compile
time than a simple built-in annotation would be.

my $.02,
–Arthur

Received on 2020-10-13 09:34:26