Skip to content

Instantly share code, notes, and snippets.

@outro56
Last active September 16, 2020 00:15
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 outro56/c741ee9497ac5342ea39928ff71d4cd8 to your computer and use it in GitHub Desktop.
Save outro56/c741ee9497ac5342ea39928ff71d4cd8 to your computer and use it in GitHub Desktop.
#include "overload.hpp"
#include <iostream>
#include <string>
#include <variant>
int main() {
using namespace cmuoh;
std::variant<int, float, std::string> intFloatString { "Hello" };
std::visit(overload {
[](const int& i) { std::cout << "int: " << i; },
[](const float& f) { std::cout << "float: " << f; },
[](const std::string& s) { std::cout << "string: " << s; }
},
intFloatString
);
}
#ifndef CMUOH_OVERLOAD_HPP_
#define CMUOH_OVERLOAD_HPP_
namespace cmuoh {
template <class ...Fs>
struct overload : Fs... {
template <class ...Ts>
overload(Ts&& ...ts) : Fs{std::forward<Ts>(ts)}... {}
using Fs::operator()...;
};
template <class ...Ts>
overload(Ts&&...) -> overload<std::remove_reference_t<Ts>...>;
} //namespace "cmuoh"
#endif //CMUOH_OVERLOAD_HPP_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment