gul-cpp / gul14

General Utility Library for C++14
https://gul14.info/
GNU Lesser General Public License v2.1
2 stars 1 forks source link

Add OverloadSet and make_overload_set() #54

Closed alt-graph closed 1 year ago

alt-graph commented 1 year ago

This PR adds the OverloadSet variadic class template and a make_overload_set() helper function to create objects of this type. Such a type is recommended for use with std::visit by Bjarne Stroustrup in "A Tour of C++" where he writes that "[t]he overloaded class is necessary and strangely enough, not standard". Well, at least we should have it. :)

I think we should squeeze this in as patch release 2.9.2. Yes, we enhance the API, but since this is header-only stuff, users can safely ignore it.

[why]

Without C++17, we do not have "if constexpr". This can make it hard to construct a suitable visitor function for gul14::visit(). The alternative to using "if constexpr" is constructing an overload set of functions.

[how]

Provide a variadic class template OverloadSet that inherits from an arbitrary number of function objects (typically lambdas) and imports their definitions of operator() into an overload set for its own operator(). Because constructing such an OverloadSet object manually is almost impossible without C++17's advanced template argument deduction, we also provide a helper function make_overload_set() to take care of that.

soerengrunewald commented 1 year ago

Looks reasonable to me, although I have no experience with it.