match them std::variants with ease
This provides two functions: match and match_mut.
match: allows you to access variants of astd::variantin aconstway.match_mut: allows you to modify variants of astd::variant.- You can even return data from the lambdas!
#include "../machstic.hpp"
#include <iostream>
int main() {
std::variant<int, float> number = (int)69;
match(number,
[](const int& integer) {
std::cout << "Integer: " << integer << std::endl;
},
[](const float& float_num) {
std::cout << "Float: " << float_num << std::endl;
}
);
// you can return data (optionally)
int num = match_mut(number,
[](int& integer) {
integer = 420;
return integer;
},
[](float& float_num) {
float_num = 100.0;
return (int)float_num;
}
);
std::cout << "Modified number: " << num << "\n";
return 0;
}More examples in examples/ directory. You can build them with make example, and view the executables (and source code) in examples/
Note: by including machstic.hpp you are automatically including <variant>,
so you don't have to manually include that.
This should be zero-cost and compile-time resolved, so you won't get a big performance hit from it.