C++17 standard is amazing with a lot of new features, and one of the interesting features was the new type std::any
. std::any
is a type-safe container to store a single value of any variable type. In this post, we explain std::any in modern C++.
What is std::any in C++ 17 ?
The any
class (std::any
) is a new class defined in the <any>
header in C++17 and it is used for any type definition, it is a safe type container for single values that are copy constructible. The std::any
is a container type that is used to store any value in it without worrying about the type safety of the variable. It has been designed based on boost::any
from the boost library. It is very useful, when you have a variable, and you want to change its type (int
to float
) on runtime.
Here is the simplified syntax for std::any
.
1 2 3 |
std::any <variable_definition>; |
Here is a simple definition example.
1 2 3 |
std::any a; |
The std::any
is a type-safe container that has properties such as has_value()
, type()
, type().name()
; it has modifiers such as emplace
, reset
, swap
; it has bad_any_cast
helper class, and it can be used with other methods such as make_any
, any_cast
, std::swap
.
How can we use std::any in C++ 17?
Here is a simple example how we can use the std::any
with different types in C++17 and beyond.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <any> int main() { std::any a; a = true; // boolean a = 100; // integer a = 9.81; // double } |
In some definitions, we can use literals to define type of the variable, let’s see example below.
Is there a full example about how can we use std::any in C++ 17?
Here is a full example about std::any that shows different any definitions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <string> #include <bitset> #include <vector> #include <any> using namespace std::literals; int main() { std::any a; std::cout << "it is " << a.type().name() << std::endl; a = true; // boolean std::cout << std::boolalpha << std::any_cast<bool>(a) << " is " << a.type().name() << std::endl; a = 100; // integer std::cout << std::any_cast<int>(a) << " is " << a.type().name() << std::endl; a = 9.81; //double std::cout << std::any_cast<double>(a) << " is " << a.type().name() << std::endl; //std::cout << std::any_cast<float>(a) << a.type().name() << std::endl; std::any a0; // empty std::any a1{}; // empty std::any a2 = {}; // empty std::any x0{ 77 }; // int std::any x1 = 4.5; // double std::any x2{ "LearnCPlusPlus.org" }; // const char * std::any x3{ "This is a string"s }; // std::string std::any x4{ std::bitset<8>{} }; //std::bitset<8> std::any x5{ std::vector<double>{9.2, 8.1, 7.3} }; //std::vector<double> system("pause"); return 0; } |
Here is the output,.
1 2 3 4 5 6 |
it is v true is b 100 is i 9.81 is d |
v shows it is any variable (empty),
b shows it is a boolean,
i shows it is an integer
d shows it is a double;
We should note that, in this example, the output could be different in different compilers in different operating systems.
For more details about this feature in C++17 standard, please see these papers; P0220R1, P0032R3, P0504R0
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.