C++ Logo

std-proposals

Advanced search

Remove restriction on deduction guides.

From: Bengt Gustafsson <bengt.gustafsson_at_[hidden]>
Date: Sat, 20 Feb 2021 15:13:39 +0100
I just noted that a deduction guide needs the stated return type to be
exactly  the name of the class template whose instance is being deduced,
a metafunction call is not allowed. In my use case I wanted a class
template to adapt to the signature of a callable sent as constructor
parameter. This does not seem to be possible in C++20.

I don't see a compelling reason to require the class template name to be
written out verbatim after the -> of the deduction guide, at this point
in the parsing the compiler knows that this is a deduction guide and
which class template it is connected to, so it should be able to parse
the rest exactly as if it was a trailing return type.

Here is the short form code, a complete example is at godbolt
(https://godbolt.org/z/x91Pz6). All major compilers give similar error
messages...

template<typename... Pars> class receiver {
public:
using Func = std::function<void(Pars...)>;

     receiver(Func function) : m_function(std::move(function)) {}
private:
     Func m_function;
};

template<typename F> receiver(F&&)->*detail::corresponding_type<receiver,
F>*;

The bold part offends the compiler although this meta function call
yields a receiver template instance. During phase 1 parsing the compiler
can only know that this yields a type (a typename specifier may be
needed in some cases) but that should be enough for it to continue.

I can't figure out a workaround that allows me to create a receiver like
this given this limitation:

receiver rec([](int, float) {doSomething(); });

I think this is a pity.

Bengt Gustafsson

PS: I just found on GodBoilt an experimental Clang version called NDSMI
which allows auto and CTAD on data members. Does anyone know who did
this and if it has a corresponding proposal? Such a feature would fit
nicely with the relaxation I refer to here, to allow wrapping lambdas in
member constructors and similar.

Received on 2021-02-20 08:13:51