AlgoMaster Logo

std::variant (C++17)

Last Updated: June 6, 2026

Medium Priority
39 min read

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.

Premium Content

Subscribe to unlock full access to this content and more premium articles.