In the previous article, we have discussed about How to Copy all Values from a Map to a Vector in CPP. Let us Learn boost::any Usage in C++ Program.
Boost::any in C++
1)Boost::any
Strongly typed languages, such as C++, require each variable to have a particular type that determines what kind of data it can store. Other programming languages, such as JavaScript, allow developers to store any type of data in variables. In JavaScript, for example, a single variable may contain a string, then a number, and finally a boolean value.
boost::any provides the boost::any class, which, like JavaScript variables, can store arbitrary types of data.
#include<boost/any.hpp>
boost::any variable_name;
Variables of type boost::any are not entirely limitless in terms of what they can store; there are some, although minor, preconditions. Any value contained in a boost::any variable must be copy-constructible. As a result, since C/C++ arrays are not copy-constructible, they cannot be stored.
2)Prebuilt Functions of Boost::Any
- clear( ) : It is used to remove the data from a variable.
- empty() : It is used to determine whether or not a variable is empty. This function is usually used in conjunction with if-else conditions.
- swap( ) : It is used to swap the contents in two variables of any datatype.
- type( ) : When we need to know what kind of data a variable contains, we use it.
- any_cast( ): This function returns a copy of the variable and is commonly used for printing.
- bad_any_cast( ) : Where the data does not fit the template datatype and an error occurs, is commonly used for try and capture blocks.
3)Implementation of boost::any
Below is the implementation:
#include "boost/any.hpp" #include <bits/stdc++.h> using namespace std; int main() { // declaring any data type boost::any a, b, c, d; // Initializing "a" with some random integer value a = 100; // Printing the value of a using boost::any_cast cout << boost::any_cast<int>(a) << endl; // Initializing "b" with some random character value b = 'S'; // Printing the value of b using boost::any_cast cout << boost::any_cast<char>(b) << endl; // Initializing "c" with some random string value c = string("BTechGeeks"); // Printing the value of c using boost::any_cast cout << boost::any_cast<string>(c) << endl; // Initializing "d" with some random float value d = 98.7423; // Printing the value of d using boost::any_cast cout << boost::any_cast<double>(a) << endl; // Giving integer value to b and providing float as // parameter for any_cast // it gives error so we used try and catch block // to handle the error try { boost::any b = 100; cout << boost::any_cast<float>(b) << endl; } catch (boost::bad_any_cast& e) { cout << "Exception is Caught while converting : " << e.what() << endl; } return 0; }
Output:
100 S BTechGeeks Exception is Caught while converting : boost::bad_any_cast: failed conversion using boost::any_cast
Related Programs:
- how to erase elements from a list in cpp using iterators
- cpp how to reverse a list or sub list in place
- cpp how to get element by index in list
- implementing a case insensitive stringfind in cpp
- unary operator overloading in cpp using both member friend function
- cpp unordered_map erase elements while iterating in a loop
- how to measure execution time in cpp