Created
April 17, 2017 08:52
-
-
Save boxdot/c27984137bb2e0bd3294eb5482af7967 to your computer and use it in GitHub Desktop.
Static polymorphism
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 <mapbox/variant.hpp> | |
#include <cstdlib> | |
#include <iostream> | |
struct A { | |
int x = 0; | |
}; | |
struct B { | |
int x = 1; | |
}; | |
int main(int argc, char const *argv[]) { | |
if (argc != 2) { | |
return 1; | |
} | |
int i = std::atoi(argv[1]); | |
mapbox::util::variant<A, B> v; | |
if (i == 0) { | |
v = A(); | |
} else { | |
v = B(); | |
} | |
v.match([](auto value) { std::cout << value.x << std::endl; }); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment