cpprussia C++ Russia 2020 СПб (11.11.2020 — 14.11.2020)

Unique_pseudofunction — N overloads for the price of one

img

This talk will present a class template that behaves almost like any_invocable, but lacks one of its key restrictions: having just one overload of operator().

This talk will present a class template that behaves almost like any_invocable, but lacks one of its key restrictions: having just one overload of operator().

std::function is an excellent tool for interface design. It encapsulates almost any object that is invocable the right way. any_invocable is an even better tool. It can encapsulate some types that std::function can’t — the ones that are move-only. Unfortunately, it is still invocable in just one way. You get one list of argument types and that’s what you must pass.

What if the object you want to pass is invocable in more than one way? What if it’s operator() is a template that you will instantiate with N different types? What if it is a variadic template to which you will pass 0-N arguments? What if you use tag dispatch to do N different things?

If you want these things, as well as the type erasure offered by std::function and the move-only support offered by any_invocable, then unique_pseudofunction is the class for you.

Follow the path Filipp took towards realizing that unique_pseudofunction is the solution to most problems. We’ll break this down into the following steps:

  • building a simple non-generic any_invocable;

  • making it generic;

  • adding a small buffer optimization;

  • extending it to support more than one overload of operator().

We will also look in close detail at several problems solved along the way, and several others that are still unsolved.

This talk involves small amounts of C++17 and large amounts of template metaprogramming, function references, and type erasure.