C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Initializing std::function with overload sets

From: Bjorn Reese <breese_at_[hidden]>
Date: Thu, 27 Feb 2025 15:15:51 +0100
On 2/27/25 03:38, Jack O'Donohue via Std-Proposals wrote:
> I noticed that unlike when initializing function pointers, std::function
> (and copyable_function, etc.) can't select the right overload of a
> function when it is initialized. For example,

The core problem is not std::function, but rather overloaded functions.
The same problem occurs when we pass an overloaded function as argument
to another function, such as std::apply.

A solution to this problem is to wrap the overloaded function as a
function object (similar to std::plus etc.)

   inline constexpr struct {
     template <typename... Args>
     auto operator()(Args&&... args) const {
       return add(forward<Args>(args)...);
     }
   } my_add{};

It is possible to create macros for this, such as

 
https://www.boost.org/doc/libs/release/libs/hof/doc/html/include/boost/hof/lift.html

Hopefully reflection will enable us to lift overloaded functions
directly.

Received on 2025-02-27 14:15:55