July 3rd, 2024

A Type for Overload Set

The article explores C++ overload set challenges, discussing issues with standard functions encountering problems due to overloading. It introduces proposal P3312 for a unique type to address these limitations, emphasizing the need for a more efficient solution.

Read original articleLink Icon
A Type for Overload Set

In a detailed exploration of C++ programming challenges, the article delves into issues with overload sets and the limitations they pose in the language. It discusses various scenarios where standard functions like std::to_string and std::min encounter problems due to overloading, leading to workarounds involving lambdas. The text highlights the proposal P3312 - Overload Set Types, aiming to introduce a unique type for overload sets to address these issues. The article also presents macro solutions like OVERLOAD for free functions and function templates, but notes the complexity in handling overloaded member functions. Despite the power of lambdas in resolving such challenges, the need for a dedicated type for overload sets is emphasized for a more elegant and efficient solution. The conclusion suggests that while the proposal holds promise, the intricacies of overloading may delay its implementation in C++, underscoring the continued importance of leveraging lambdas for resolving errors in the meantime.

Link Icon 3 comments
By @captainmuon - 5 months
I'm really surprised that the first example doesn't work. I would have thought that it decides which overload to use only when instantiating the template, and then doing argument dependent lookup (or whatever it is called) to pick the correct overload to match the type of the elements.

On the other hand, I don't even know how transform is defined. C++ has nothing exactly like IEnumerable<T>, so maybe it is hard to refer to the T? And of course what the article references, that there is no way to refer to the overload set.

I usually like C++, but it is not fun when the abstractions break like that.

By @mmaniac - 5 months
> The crux of the problem, where all the examples above share in common, is that C++ does not have a type for overload sets.

>There is a recent proposal that aims to address this exact issue: P3312 - Overload Set Types.

Painfully common story.

By @Trung0246 - 5 months
This is my own version of `OVERLOAD` that works with both, although I forgot how did it works:

    #define FWD(...) ::std::forward<decltype(__VA_ARGS__)>(__VA_ARGS__)

    #define LIFT_CUSTOM(X, CAPTURE) CAPTURE (auto &&... args) \
     noexcept(noexcept(X(FWD(args)...))) -> decltype((X(FWD(args)...))) { \
      return X(FWD(args)...); \
     }

    #define LIFT(X) LIFT_CUSTOM(X, [&])