Skip to content

Instantly share code, notes, and snippets.

@g3rrit
Created July 9, 2020 20:11
Show Gist options
  • Select an option

  • Save g3rrit/aca8b9c6f79e65187cc21656cb31b4fb to your computer and use it in GitHub Desktop.

Select an option

Save g3rrit/aca8b9c6f79e65187cc21656cb31b4fb to your computer and use it in GitHub Desktop.
#include <variant>
#include <iostream>
#include <tuple>
#include <type_traits>
template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
int main()
{
std::variant<int*, int, std::tuple<int, int*>> var { std::make_tuple(10, new int(10)) };
std::visit(overload {
[] (auto& c) {
return;
},
[] (auto* c) {
delete c;
},
[] <class... Args> (std::tuple<Args...>& t) {
std::apply([] (Args&... args) {
([] <class T> (T t) { if constexpr (std::is_pointer<T>::value) { delete t; } }(args), ...);
}, t);
}
}, var);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment