Skip to content

Instantly share code, notes, and snippets.

@talybin
Last active August 27, 2020 13:02
Show Gist options
  • Select an option

  • Save talybin/13d595eb17b0c4142cc4c38eed31a7ee to your computer and use it in GitHub Desktop.

Select an option

Save talybin/13d595eb17b0c4142cc4c38eed31a7ee to your computer and use it in GitHub Desktop.
Lambda closure decomposition
#include <iostream>
template <class T> struct overload : T { using T::operator(); };
template <class T> overload(T) -> overload<T>;
int main()
{
auto f = [x = 1, y = 2]() { return x + y; };
// auto [a, b] = f; // error: cannot decompose lambda closure type 'main()::<lambda()>'
overload o { f };
auto [a, b] = o;
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
}
@talybin

talybin commented Aug 27, 2020

Copy link
Copy Markdown
Author
a: 1
b: 2

@talybin

talybin commented Aug 27, 2020

Copy link
Copy Markdown
Author

Works with gcc only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment