Created
July 9, 2020 20:11
-
-
Save g3rrit/aca8b9c6f79e65187cc21656cb31b4fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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