Gudmundur F. Adalsteinsson – 2020-01-05 – Blog index

Overloading Lambdas and Sharing State

Creating function overload sets with lambdas and library solutions to share state between overloads.

Introduction

A common problem people face when using std::visit is that it takes a std::variant and a single visitor function object, and therefore is’s not straight-forward to pass lambdas to it like you can with almost any other higher-order algorithm in the standard library. There exists a documented pattern (also available in Boost.Hana) that uses C++17 to solve the problem with an overload class:

template < class ... Ts> struct overload : Ts... ... Ts>overload : Ts... { using Ts:: operator ()...; Ts::()...; }; template < class ... Ts> overload(Ts...) -> overload<Ts...>; ... Ts> overload(Ts...) -> overload ;

A similar problem is related to callbacks to async (or sync) functions that can fail with an error or return a result value (see p1678 (Callback and Composition) for more details). These callbacks or handlers often need to share state (think some non-trivial resources), so there is something to be gained in both space and time compared to the simple overload class above.

Overload sets have been called the atoms of C++ API design. But any solution to passing overload sets around don’t solve the problem we are facing here, i.e. constructing overloaded lambda closures.

Overloading operator()

For an example, we will start with a function foo that takes a function object that either takes two int or a string-like object. The function object will capture a variable of type A by reference and an integer, and will print them via member functions of A .

template < class F> F> void foo( int i, F f) foo(i, F f) { if (i > 0 ) (i > 2 ); f(i,); else std:: string_view( "error" )); f(string_view()); } struct A { void print_int( int i) const print_int(i) { std:: cout << "got int " << i << std:: endl; cout <<<< i < } void print_string( std:: string_view str) const print_string(string_view str) { std:: cout << "got string " << str << std:: endl; cout <<<< str < } };

We will then execute the examples below like so:

A a; 2 ); exampleN(a,);

Using if constexpr

We can use C++17’s if constexpr syntax to write a single lambda to call the correct function. The size of the overload is 16 bytes (a reference plus an int ) which is as small is it gets.

template < class T> T> void example1(A& a, T c) example1(A& a, T c) { auto f = [&a, c]( auto && ...args) { f = [&a, c](&& ...args) { auto ma = [c]( int x, int y) { return x + y * c; }; ma = [c](x,y) {x + y * c; }; if constexpr ( sizeof ...(args) == 1 ) ...(args) == a.print_string(args...); else a.print_int(ma(args...)); }; std:: cout << "example1 size " << sizeof f << std:: endl; cout <<< 38 , f); foo(, f); 1 , f); foo(-, f); }

example1 size 16 size 16 got int 42 int 42 got string error string error

Using overload

Now we use the overload class, which makes the code readable, but the size is now 24 bytes, since the reference to a is captured twice.

template < class ... Ts> struct overload : Ts... ... Ts>overload : Ts... { using Ts:: operator ()...; Ts::()...; }; template < class ... Ts> overload(Ts...) -> overload<Ts...>; ... Ts> overload(Ts...) -> overload ; template < class T> T> void example2(A& a, T c) example2(A& a, T c) { auto f = overload{ f = overload{ int x, int y) { [&a, c](x,y) { a.print_int(x + y * c); }, std:: string_view str) { [&a](string_view str) { a.print_string(str); } }; std:: cout << "example2 size " << sizeof f << std:: endl; cout <<< 38 , f); foo(, f); 1 , f); foo(-, f); }

Local struct

To minimize the size of the overload we can define and construct a struct locally, bringing us back to 16 bytes at the expense of readability.

template < class T> T> void example3(A& a, T c) example3(A& a, T c) { struct { A& a; T c; void operator ()( int x, int y) const { a.print_int(x + y * c); } ()(x,y){ a.print_int(x + y * c); } void operator ()( std:: string_view str) const { a.print_string(str); } ()(string_view str){ a.print_string(str); } } f{a, c}; std:: cout << "example3 size " << sizeof f << std:: endl; cout <<< 38 , f); foo(, f); 1 , f); foo(-, f); }

Local struct with a template

We can not define template function inside function scope (why not?), so if we need to overload generic functions we need to move the struct definition outside the function. A bit less readable than the previous example.

template < class T> T> struct Op { Op { A& a; T c; void operator ()( int x, int y) const { a.print_int(x + y * c); } ()(x,y){ a.print_int(x + y * c); } template < class Str> Str> void operator ()( const Str& str) const { a.print_string(str); } ()(Str& str){ a.print_string(str); } }; template < class T> T> void example4(A& a, T c) example4(A& a, T c) { Op f{a, c}; std:: cout << "example4 size " << sizeof f << std:: endl; cout <<< 38 , f); foo(, f); 1 , f); foo(-, f); }

Using stateful_overload

We can come up with a library solution similar to overload that explicitly captures state of any type.

A capture of a single variable would make this more readable (or if structured bindings were allowed in the parameters list). If we capture a single variable by reference the code might look like this

auto f = stateful_overload{ f = stateful_overload{ std:: ref(a), ref(a), int x, int y) { [](A& a,x,y) { a.print_int(x + y); }, std:: string_view str) { [](A& a,string_view str) { a.print_string(str); } };

Capturing this is not quite as simple as with lambdas.

class B { public : void pub() pub() { auto f = stateful_overload{ f = stateful_overload{ this , auto s, int x, int y) { [](s,x,y) { s->priv(x + y); }, auto s, std:: string_view str) { [](s,string_view str) { s->priv(str); } }; 38 , f); foo(, f); } private : template < class T> void priv(T x) {} T>priv(T x) {} };

Using bind_front and overload

You might have noticed that the stateful_overload solution looks a lot like partial currying. In C++20 we will get std::bind_front that does exactly that.

template < class T> T> void example6(A& a, T c) example6(A& a, T c) { auto f = std:: bind_front(overload{ f =bind_front(overload{ auto & a, auto c, int x, int y) { [](& a,c,x,y) { a.print_int(x + y * c); }, auto & a, auto c, std:: string_view str) { [](& a,c,string_view str) { a.print_string(str); } std:: ref(a), c); },ref(a), c); std:: cout << "example6 size " << sizeof f << std:: endl; cout <<< 38 , f); foo(, f); 1 , f); foo(-, f); }

example6 size 24 size 24 got int 42 int 42 got string error string error

It looks like it’s a quality of implementation issues (gcc 9.2) since the size is now 24 bytes instead of the expected 16 bytes. If we take the (simplified) definiton for the return type of bind_front and add [[no_unique_address]] in front of the captured function object we save 8 bytes:

template < typename _Fd, typename ... _BoundArgs> _Fd,... _BoundArgs> struct _Bind_front _Bind_front { template < typename _Fn, typename ... _Args> _Fn,... _Args> _Bind_front(_Fn&& __fn, _Args&&... __args) std:: forward<_Fn>(__fn)) : _M_fd(forward (__fn)) std:: forward<_Args>(__args)...) , _M_bound_args(forward (__args)...) {} // ... no_unique_address ]] _Fd _M_fd; [[]] _Fd _M_fd; std:: tuple<_BoundArgs...> _M_bound_args; tuple _M_bound_args; };

I’m not sure if the specification allows this, maybe a more knowledgeable reader can answer that.

Conclusion

The preferred method to construct function overloads and share state is the combination of two already available library solutions: bind_front and overload . The only drawback is the (possibly) non-optimal size. If that is a problem you can always go for the custom stateful_overload or local struct methods.