C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 18 Jul 2023 12:09:15 +0100
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.

Received on 2023-07-18 11:09:28