C++ Logo

std-proposals

Advanced search

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

From: Robin Savonen Söderholm <robinsavonensoderholm_at_[hidden]>
Date: Thu, 27 Feb 2025 17:50:28 +0100
Or simply use an 'inline constexpr lambda':
inline constexpr add = [] <typename T> (T1 lhs, T2 rhs) { return lhs + rhs;
};

// Robin

On Thu, Feb 27, 2025, 15:15 Bjorn Reese via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> 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.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-02-27 16:50:41