Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexey-N-Chernyshov/6d5199ca89e77a09da51720094157d46 to your computer and use it in GitHub Desktop.
Save Alexey-N-Chernyshov/6d5199ca89e77a09da51720094157d46 to your computer and use it in GitHub Desktop.
Inplace visitor for boost::variant
/**
* Inplace visitor for boost::variant.
* Usage:
* boost::variant<int, std::string> value = "1234";
* ...
* visit_in_place(value,
* [](int v) { std::cout << "(int)" << v; },
* [](std::string v) { std::cout << "(string)" << v;}
* );
*/
template <typename TVariant, typename... TVisitors>
auto visit_in_place(TVariant &&variant, TVisitors &&... visitors) {
return boost::apply_visitor(
boost::hana::overload(std::forward<TVisitors>(visitors)...),
std::forward<TVariant>(variant));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment