C++C++17C++20Introduction to C++Learn C++Syntax

What Is Std::any In C++ 17 And How We Can Use It?

What Is Stdany In C++ 17 And How We Can Use It

C++17 standard is amazing with a lot of new features, and one of the interesting features was the new type std::anystd::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.

Here is a simple definition example.

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.

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.

Here is the output,.

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; P0220R1P0032R3P0504R0

What Is Stdany In C++ 17 And How We Can Use It

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++