C++ Logo

sg10

Advanced search

Re: [SG10] [isocpp-core] Feature-test macro for ADL calls with template arguments?

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Tue, 9 Jun 2020 06:59:38 -0400
On Mon, Jun 8, 2020 at 9:22 PM Ville Voutilainen via Core <
core_at_[hidden]> wrote:

> On Tue, 9 Jun 2020 at 03:55, Peter Dimov <pdimov_at_[hidden]> wrote:
> >
> > Ville Voutilainen wrote:
> >
> > > And *callers* do no using-declarations, and no ifdeffing.
> >
> > This will only work if get<> is visible to normal lookup. (Well it works
> > under Clang, with a warning that it's C++20 only.)
> > https://godbolt.org/z/Z3LkwJ
>
> So? John's work-around makes get<> visible to normal lookup, thus:
> https://godbolt.org/z/Zvqjze
>
> With C++20 in hand, the work-around can be removed.


Richard has already shown a reason the "work-around" cannot in general be
removed:
https://godbolt.org/z/gFHK-2

struct get {};

int fail_in_cpp20()
{
  QtPair tp{ 1, 2 };
  // return get<0>( tp ); // fails to compile
}

int succeed()
{
  QtPair tp{ 1, 2 };
  using std::get; // the "std::swap two-step" for calling ADL-only
functions
  return get<0>( tp );
}

The programmer (the caller) already has to do the "two-step" in C++17 — in
fact they've had to do it since C++98 — and the need to do it doesn't go
away in C++20. (The caller can now get away with skipping the two-step in
more cases, but only if they trust their coworkers never to change stuff at
a distance in a way that breaks their shortcut.) From *your *point of view
as the *implementor *of QtPair, you don't have to do anything special;
C++20 hasn't added any new functionality for the implementor. C++20 merely
added a new *optional shortcut* for *callers *willing to trade
maintainability for one line of code savings.

–Arthur

Received on 2020-06-09 06:02:58