# **[](https://en.cppreference.com/w/cpp/header/variant)** is a type-safe union: it holds a value of one of the possible types (C++17). Use `std::holds_alternative(v)` to check which type is held, and `std::get(v)` to access the value (throws if wrong type). It's more elegant than `std::any` when you know the set of possible types in advance. ## Example This example returns a variant result and safely checks which type is held before accessing it. ```cpp // compile: g++ -std=c++17 -o variantexample variantexample.cpp // run: ./variantexample // description: type-safe union with variant #include #include #include std::variant process() { return std::string("hello"); } int main() { auto result = process(); if (std::holds_alternative(result)) { std::cout << "got string: " << std::get(result) << "\n"; } return 0; } ```