C++17 Library

MultiVariant Recursive Visitor

A professional-grade, type-safe solution for multi-dispatch and recursive visitation, designed to elegantly handle complex use cases of `std::variant` in C++17+.

Language C++17
Type Header-only
License MIT
Dependencies None

Why use this library?

Standard C++ does not provide native pattern matching for multiple variants. `std::visit` only covers limited cases and quickly becomes complex to use with tuples or dynamic packs.

This library abstracts away that complexity, allowing you to write expressive and type-safe code.

Key Features

Multi-Variant Dispatch

Visit N `std::variant` values simultaneously and recursively.

Tuple Support

Elegant dispatch on `std::tuple` of `std::variant`s with `multi_visit_tuple`.

MultiDispatcher

Automatically selects the best overload at compile time using `is_invocable`.

Header-Only

No external dependencies, no Boost, no macros. Easy to integrate.

Fully Tested

Comprehensive coverage of use cases: move-only, constexpr, exception safety, etc.

Type-Safe

Leverages the C++ type system to guarantee correctness at compile time.

Usage Examples

Visit several variants simultaneously:


std::variant a = 42;
std::variant b = true;

multi_visit([](auto x, auto y) {
    std::cout << "Dispatch: "
              << typeid(x).name() << ", "
              << typeid(y).name() << "\n";
}, a, b);

Or on tuples of variants:


auto tuple = std::make_tuple(
    std::variant{3.14},
    std::variant{'Z'}
);

multi_visit_tuple(
    overloaded{
        [](double d, char c) { std::cout << d << " and " << c; },
        [](auto...) { std::cout << "Fallback"; }
    },
    tuple
);

Installation and Tests

Installation

This is a header-only library. Simply include the main file:


#include "multi_visit.hpp"

Tests

The project includes a complete test suite with GoogleTest. To run them:

mkdir build && cd build
cmake ..
make
ctest

MIT License

This project is distributed under the MIT license. Contributions are welcome. Feel free to open an issue for any discussion or suggestion for improvement.