Skip to content

yet another C/C++ std::variant overload implementation. Header-only data-structure for use with std::visit and std::variant

License

Notifications You must be signed in to change notification settings

yalibs/yaoverload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YA-OVERLOAD

yet another C/C++ std::variant overload implementation. Header-only data-structure for use with std::visit and std::variant

Usage Notes

ya::overload is designed specifically to be used in together with std::variant and std::visit as a type-safe switch-like structure.

Using a Universal Reference

If you don't want to define all of your type cases, you can even use the universal reference, like below. Be advised that the universal reference will overrule other overloads defined after it, so make sure to put the universal reference overload as the last overload. Treat it as the default keyword in a regular switch.

std::variant<int,float,bool> example_variant = 3;
std::visit(ya::overload(
        [](auto&& v){ std::cout << v << std::endl; },
       ), example_variant);

Example Usage

#include <overload> // include the library
std::variant<int,float,bool> example_variant = 3;
std::visit(ya::overload(
        [](const int& i){ std::cout << i << " integer"; },
        [](const float& f){ std::cout << f << " float"; },
        [](const bool& b){ std::cout << std::boolalpha << b << " bool"; }
       ), example_variant);

About

yet another C/C++ std::variant overload implementation. Header-only data-structure for use with std::visit and std::variant

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published