C++ Logo

sg10

Advanced search

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

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Tue, 9 Jun 2020 00:18:33 +0300
On Tue, 9 Jun 2020 at 00:01, Richard Smith <richardsmith_at_[hidden]> wrote:
> I thought the usual way you write ADL-only calls is
>
> template<typename T> void f(T t) {
> using somewhere::name;
> name(t); // ADL-only call with fallback to somewhere::name
> }

The "name" in this case is "get", and it needs to be called with a
numeric template argument.

> (eg, this is how I think we would recommend that people call 'begin' and 'end': "using std::begin; begin(x);"). I think that works equally well if 'somewhere' contains a function template, even if you don't have anything to put in namespace 'somewhere':
>
> namespace somewhere {
> template<typename T> void name(...) = delete; // specialization missing
> }
> template<typename T> void f(T t) {
> using somewhere::name;
> name<int>(t); // ADL-only call with fallback to somewhere::name
> }
>
> This works without a feature test macro, and is more reliable than relying on the new feature. (For example, the above pattern is not broken if someone incautiously adds a non-function, non-function-template 'name' to a scope enclosing 'f', whereas use of the new feature would be broken by that.)

All we want is to be able to write QPair<What, Ever> qp; get<1>(qp);

I don't know what that "somewhere" would be, nor do I know why I'd
write a using-declaration for somewhere::get.

Received on 2020-06-08 16:21:53