Uniform initialization with std::any in C++

In C++, the std::any class provides a type-safe and flexible way to store a value of any type. It can be used when you need to store values of different types without explicitly specifying the type beforehand. The introduction of std::any in C++17 simplifies the task of working with heterogeneous data structures.

Initialization using std::any

To initialize a std::any object, you can use uniform initialization syntax. Here’s an example:

#include <any>

std::any value1{42};
std::any value2{3.14};
std::any value3{std::string("Hello")};

In this example, three std::any objects value1, value2, and value3 are initialized with different values of different types: an int, a double, and a std::string, respectively.

Accessing the value stored in std::any

To access the value stored in a std::any object, you can use std::any_cast with the type you expect the value to be. If the stored value is not of the specified type, a std::bad_any_cast exception will be thrown.

#include <any>
#include <iostream>

std::any value{42};

try {
    int num = std::any_cast<int>(value);
    std::cout << "Value: " << num << std::endl;
} catch (const std::bad_any_cast& e) {
    std::cout << "Failed to cast to int: " << e.what() << std::endl;
}

In this example, we access the value stored in the std::any object value and try to cast it to int. If the cast is successful, we print the value. Otherwise, we catch the std::bad_any_cast exception and print an error message.

Benefits of uniform initialization with std::any

  1. Flexibility: Instead of defining a specific data structure to hold values of different types, you can simply use std::any to store values of any type.
  2. Type safety: Since the type information is preserved, you can ensure type correctness when accessing the stored value.
  3. Simplified code: The use of std::any and uniform initialization allows for cleaner and more concise code when dealing with heterogeneous data.

Uniform initialization with std::any provides a convenient and type-safe way to store and access values of different types in C++. It is a powerful tool for working with heterogeneous data structures and simplifies the handling of variant type scenarios.

References: