Table of Contents
Understanding std::variant Basics
Accessing Values Safely
Use std::get or std::get
Check Active Type with std::holds_alternative
Using std::visit for Type-Safe Dispatch
Handling Errors and Edge Cases
Tips and Best Practices
Home Backend Development C++ How to work with std::variant in C

How to work with std::variant in C

Aug 14, 2025 am 11:32 AM
c++

std::variant is a type-safe union introduced by C 17. It can safely hold values of one of the specified types. It can realize secure access and type checking through methods such as std::get, std::holds_alternative, std::visit and std::get_if. Combined with std::monostate, optional values can be simulated. It is recommended to use std::visit for type distribution and avoid large type lists to improve maintainability, and ultimately ensure type safety and exception safety.

How to work with std::variant in C

Working with std::variant in C involves safely handling a type-safe union that can hold one of several specified types at a time. It was introduced in C 17 as a modern, safer alternative to raw unions. Here's how to use it effectively.

Understanding std::variant Basics

std::variant is a template that can store one value from a fixed list of types. Only one type is active at a time, and the variant keeps track of which one.

 #include <variant>
#include <string>
#include <iostream>

std::variant<int, std::string, double> v;
v = 42; // v holds an int
v = "hello"; // v holds a std::string

By default, the variant is initialized with the first type in the list (if it's default-constructible). In the example above, v starts as an int(0) .

You can also explicitly construct it:

 std::variant<int, std::string> v = std::string("hello");

Accessing Values Safely

Never assume which type is stored. Use proper methods to check or retrieve values.

Use std::get<T> or std::get<I>

You can retrieve the value if you know the type or index:

 std::get<int>(v); // if v holds int
std::get<0>(v); // same, by index

But if the type doesn't match, it throws std::bad_variant_access . So always check first.

Check Active Type with std::holds_alternative

 if (std::holds_alternative<int>(v)) {
    std::cout << "int: " << std::get<int>(v);
} else if (std::holds_alternative<std::string>(v)) {
    std::cout << "string: " << std::get<std::string>(v);
}

This is safe and clear for simple cases.

Using std::visit for Type-Safe Dispatch

The most powerful way to work with std::variant is std::visit , which applies a callable to the active value.

 std::visit([](auto&& arg) {
    using T = std::decay_t<decltype(arg)>;
    if constexpr (std::is_same_v<T, int>) {
        std::cout << "int: " << arg;
    } else if constexpr (std::is_same_v<T, std::string>) {
        std::cout << "string: " << arg;
    } else if constexpr (std::is_same_v<T, double>) {
        std::cout << "double: " << arg;
    }
}, v);

This is clean and avoids runtime type checks when possible (thanks to if constexpr ).

You can also define a visitor struct:

 struct Printer {
    void operator()(int i) const { std::cout << "int: " << i; }
    void operator()(const std::string& s) const { std::cout << "string: " << s; }
    void operator()(double d) const { std::cout << "double: " << d; }
};

std::visit(Printer{}, v);

This works because the visitor must be callable for every possible alternative in the variant.

Handling Errors and Edge Cases

  • Assignment : Assigning a new value changes the active type automatically.
  • No value? Unlike std::optional , std::variant always holds a value (of one of the types).
  • Exception safety : Construction and assignment may throw if the target type's operations throw.
  • Empty variant? There's no “empty” state unless you include std::monostate for emulating optional behavior in a variant.

Example with std::monostate :

 std::variant<std::monostate, int, std::string> maybe_value;
// Initially holds std::monostate → "empty"

Tips and Best Practices

  • Keep variant types small and related logically (eg, AST node types, message variants).
  • Prefer std::visit over repeated holds_alternative checks when doing complex logic.
  • Avoid very large variant lists — they become hard to maintain and may impact performance.
  • Make sure all visitor overloads are defined; otherwise, compilation fails.
  • Use std::get_if<T>(&v) to get a pointer to the value if it's the right type — useful for checking without exceptions:
 if (auto* p = std::get_if<int>(&v)) {
    std::cout << "Got int: " << *p;
}

This is especially useful in performance-critical code where exceptions are avoided.


Basically, std::variant works best when combined with std::visit and proper type-safe access patterns. It's a key tool for modern C when you need sum types or type-safe unions.

The above is the detailed content of How to work with std::variant in C. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1582
276
Succinct (PROVE Coin) Price Forecast: 2025, 2026, 2027-2030 Succinct (PROVE Coin) Price Forecast: 2025, 2026, 2027-2030 Aug 11, 2025 am 10:12 AM

Directory What is Succinct (PROVE) Which venture capital supports Succinct (PROVE)? How Succinct (PROVE) Working Principle SP1zkVM and Prover Network OPSuccinct Technology Cross-chain Verification PROVE Token Economics Token Details 2025, 2026, 2027-2030 Succinct (PROVE) Price Forecast Succinct (PROVE) Price Forecast Succinct (PROVE) Price Forecast: Trading Volume Expansion and Listing Momentum 2025-20

What should I do if the application cannot start normally (0xc0000906)? See the solution here What should I do if the application cannot start normally (0xc0000906)? See the solution here Aug 13, 2025 pm 06:42 PM

When opening the software or game, a prompt suddenly appears that "the application cannot start normally (0xc0000906)" appears, and many users will be confused and don't know where to start. In fact, most of these errors are caused by corruption of system files or missing runtime libraries. Don't rush to reinstall the system. This article provides you with several simple and effective solutions to help you quickly restore the program to run. 1. What is the error of 0xc0000906? Error code 0xc0000906 is a common startup exception in Windows systems, which usually means that the program cannot load the necessary system components or running environment when running. This problem often occurs when running large software or games. The main reasons may include: the necessary runtime library is not installed or damaged. The software installation package is endless

How to use regular expressions in C How to use regular expressions in C Aug 12, 2025 am 10:46 AM

To use regular expressions in C, you need to include header files and use the functions it provides for pattern matching and text processing. 1. Use std::regex_match to match the full string, and return true only when the entire string conforms to the pattern; 2. Use std::regex_search to find matches at any position in the string; 3. Use std::smatch to extract the capture group, obtain the complete match through matches[0], matches[1] and subsequent sub-matches; 4. Use std::regex_replace to replace the matching text, and support the capture group with references such as $1 and $2; 5. You can add an iset when constructing the regex (

How to fix missing MSVCP71.dll in your computer? There are only three methods required How to fix missing MSVCP71.dll in your computer? There are only three methods required Aug 14, 2025 pm 08:03 PM

The computer prompts "MsVCP71.dll is missing from the computer", which is usually because the system lacks critical running components, which causes the software to not load normally. This article will deeply analyze the functions of the file and the root cause of the error, and provide three efficient solutions to help you quickly restore the program to run. 1. What is MSVCP71.dll? MSVCP71.dll belongs to the core runtime library file of Microsoft VisualC 2003 and belongs to the dynamic link library (DLL) type. It is mainly used to support programs written in C to call standard functions, STL templates and basic data processing modules. Many applications and classic games developed in the early 2000s rely on this file to run. Once the file is missing or corrupted,

How to get the size of a file in C How to get the size of a file in C Aug 11, 2025 pm 12:34 PM

Use the seekg and tellg methods of std::ifstream to obtain file size across platforms. By opening a binary file and positioning it to the end, use tellg() to return the number of bytes; 2. It is recommended to use std::filesystem::file_size for C 17 and above. The code is concise and errors are handled through exceptions. The C 17 standard must be enabled; 3. On POSIX systems, the stat() function can be used to efficiently obtain file size, which is suitable for performance-sensitive scenarios. The appropriate method should be selected based on the compiler and platform, and std::filesystem should be used first (if available), otherwise use ifstream to ensure compatibility, or use st on Unix systems

C   operator overloading example C operator overloading example Aug 15, 2025 am 10:18 AM

Operator overloading in C allows new behaviors of standard operators to be assigned to custom types, 1. Return new objects through member function overloading; 2. Overload = Modify the current object and return reference; 3. Friend function overloading

How to write a basic Makefile for a C   project? How to write a basic Makefile for a C project? Aug 15, 2025 am 11:17 AM

AbasicMakefileautomatesC compilationbydefiningruleswithtargets,dependencies,andcommands.2.KeycomponentsincludevariableslikeCXX,CXXFLAGS,TARGET,SRCS,andOBJStosimplifyconfiguration.3.Apatternrule(%.o:%.cpp)compilessourcefilesintoobjectfilesusing$

How to work with std::variant in C How to work with std::variant in C Aug 14, 2025 am 11:32 AM

std::variant is a type-safe union introduced by C 17. It can safely hold the value of one of the specified types. It can realize secure access and type checking through methods such as std::get, std::holds_alternative, std::visit and std::get_if. Combined with std::monostate, optional values can be simulated. It is recommended to use std::visit for type distribution and avoid large type lists to improve maintainability, and ultimately ensure type safety and exception safety.

See all articles