Skip to content

Mrmayman/machstic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

machstic

match them std::variants with ease

This provides two functions: match and match_mut.

  • match: allows you to access variants of a std::variant in a const way.
  • match_mut: allows you to modify variants of a std::variant.
  • You can even return data from the lambdas!

Example

#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.

About

match them std::variants with ease

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors