variant

A header-only C++11/C++14 alternative to boost::variant. It provides a type-safe way to hold one of several types, such as a result or an error.

Share on XLicense: BSD-3-Clause

Overview

Mapbox Variant is a header-only alternative to boost::variant for C++11 and C++14. It gives a type-safe way to hold one of several types, for example an API response that is either a result or an error. It offers pattern matching with lambdas, visitors, and a recursive wrapper for tree-like types such as JSON. It has no dependencies.

Key features

  • Type-safe variant for sum types and discriminated unions
  • match() member takes lambdas and checks all types are handled
  • apply_visitor and get() for visiting or unwrapping values
  • recursive_wrapper supports recursive types such as JSON trees

Best for

C++11 or C++14 projects that want variant types without depending on Boost. It aims for a drop-in API with boost::variant but implements only the features actually needed.

Upstream
mapbox/variant
Fork on GitHub
Guo-astro/variant
Upstream stars
385
Category
Developer tools and infrastructure
Language
C++
License
BSD-3-Clause
Forked
2020-11-01
Sync status
In syncLast synced 2026-09-29