Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created April 17, 2017 08:52
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 boxdot/c27984137bb2e0bd3294eb5482af7967 to your computer and use it in GitHub Desktop.
Save boxdot/c27984137bb2e0bd3294eb5482af7967 to your computer and use it in GitHub Desktop.
Static polymorphism
#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