Skip to content

std_variant

serhij.zolotukhin edited this page Sep 27, 2026 · 3 revisions

dynamic typing

C++ is a statically typed language, meaning variable types are checked at compile time, but you can simulate dynamic typing (storing different types in the same variable at runtime) using std::variant (introduced in C++17)

A variant is a data type introduced in C++ 17 that can hold values of different types, much like a union in C. At its core, std::variant is a union of types. It can store one value at a time from a predefined set of types. Unlike a traditional union, std::variant keeps track of its active type, ensuring that you access the correct value.

https://www.geeksforgeeks.org/cpp/std-variant-in-cpp-17/ https://ru.cppreference.com/cpp/utility/variant https://medium.com/@weidagang/modern-c-std-variant-and-std-visit-3c16084db7dc

My side bar

Clone this wiki locally