Last Updated: June 6, 2026
std::variant<Ts...> is a type-safe tagged union. It holds exactly one value, drawn from a fixed list of types declared at compile time, and the compiler tracks which alternative is currently active. The header is <variant>, the type lives in namespace std, and the entire chapter assumes C++17 or later. Compile examples with g++ -std=c++17.