C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Template Type Resolution -- Special Rules for std::function

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Tue, 18 Jul 2023 14:31:35 +0300
On Tue, 18 Jul 2023 at 14:09, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Consider the following example code:
>
> #include <functional>
>
> void FuncA(std::function<void(int)> const &) {}
>
> template<typename R, typename... Params>
> void FuncB(std::function<R(Params...)> const &) {}
>
> void Monkey(int) {}
>
> int main(void)
> {
> FuncA(Monkey);
> FuncB(Monkey); // Fails to compile
> }
>
> I think 'std::function' should be given special treatment by compilers
> so that the above compiles.
>
> Something along the lines of:
> When the compiler is trying to instantiate a template with an
> std::function parameter, and a function pointer is passed as the
> argument, the full type of the std::function can be implicitly
> determined from the type of the function pointer.

You can just as well use CTAD here, and change it to
FuncB(std::function(Monkey));
after which it compiles just fine.

Received on 2023-07-18 11:31:49