C++ Boost Any














































C++ Boost Any



//Implementable on C++ 11/14/17
Boost Any
Description :
This class is used to store any type of arbitrary values/information. It uses the concept of dynamic storage. Any value stored in a variable that is of type boost::any must be copy-constructible. Therefore we cannot use C arrays to store, as they are not copy-constructible. If we want to store boost::any type of values in contiguous format then we can use vector. 
Header file used :
#include<boost/any.hpp>
Functions : 
>clear( ) :- used to clear the data that the variable contains.
syntax:- a.clear( );//where a is a variable of boost::any type
>empty( ) :- it is used to check whether a variable is empty or not. This function is generally used with if-else conditions.
syntax:- a.empty( )
>swap( ):- it is used to swap the content of two variables of any datatype.
syntax:- a.swap(b);//swap a with b
>type( ):- it is used when we need to know which type of data does a variable contain.
syntax:- a.type();//tells us whether variable a is integer,char or any other type of data
>any_cast( ):-returns a copy of the variable and is generally used for printing purpose.
syntax:-cout<<boost::any_cast<int>(a);//used to print the contents of variable a
>bad_any_cast( ):-is generally used with try and catch blocks when the data does not match the template datatype and an error occurs.
syntax:-boost::bad_any_cast &a//where variable a will tell what type of exception it is
Implementation :
>code for printing the contents of a vector which contains values of different types
#include <iostream>
#include<boost/any.hpp> //to include boost any library
#include<vector>//to include vector library header
int main()
{
boost::any a;
//to define a variable type of boost any
std::vector<boost::any>vec;//to define a vector that can contain any values like char,string,integer etc
std::string s1;//a normal string declaration
a =
4;//assigning a integer value to the any type variable
vec.push_back(a);
//to store any type variable in vector
a.clear();
//clears the contents of a
if (a.empty())//checks whether a is empty or not
{
std::cout << "a is empty\n";
}
a =
'z';//assigning a character value to the any type variable
vec.push_back(a);
a =
std::string("hello world!");//assigning a string value to the any type variable
vec.push_back(a);
a =
7.22;//assigning a double value to the any type variable
vec.push_back(a);
a =
true;//assigning a boolean value to the any type variable
vec.push_back(a);
for (int i = 0;i < vec.size();i++)
{
const std::type_info& ti = vec[i].type();//to get the datatypes of vector elements
s1 = ti.name();
//s1 will store string that denote datatypes like "int","string"etc
if (s1 == "int")//if v[i] is integer
{
std::cout<<boost::any_cast<int>(vec[i])<<"\n";//to access the value of boost::any variable we use boost::any_cast which returns a copy of v[i]'s data
}
else if (s1 == "double")//if v[i] is double
{
std::cout << boost::any_cast<double>(vec[i])<<"\n";
}
else if (s1 == "char")//if v[i] is character
{
std::cout << boost::any_cast<char>(vec[i])<<"\n";
}
else if(s1=="bool")//if v[i] is boolean
{
std::cout << std::boolalpha<<boost::any_cast<bool>(vec[i])<<"\n";//if we don't write std::boolalpha then the value that it will print will be 1

}
else//if v[i] is string
{
std::cout << boost::any_cast<std::string>(vec[i])<<"\n";
}
}


return 0
}
Output :
a is empty
4
z
hello world!
7.22
true
>code for swapping 2 variables of different data types
#include <iostream>
#include<boost/any.hpp>
void swap(boost::any &w,boost ::any&x)//fuction in which swapping takes place
{
w.swap(x);
//fuction of boost::any used for swapping
}
int main()
{
boost::any a =
1, b = 2.4;//assigning integer value to variable a and double value to b
swap(a, b);
//after executing this we get swapped answer i.e b will be a integer value and a will be a double value
std::cout <<"b = "<< boost::any_cast<int>(b)<<"\n";
std::cout <<"a = "<<boost::any_cast<double>(a)<<"\n";
return 0
}
Output :
b = 1
a = 2.4

>code describing the use of boost::bad_any_cast
#include <iostream>
#include<boost/any.hpp>
int main()
{
boost::any b =
1;//b contains a integer value

try {
std::cout << boost::any_cast<float>(b) << "\n";//a copy of b's data which we are trying to print by casting it to float which throws an exception


}
catch (boost::bad_any_cast& e)//exception caught
{
std::cerr << e.what() << "\n";//cerr is extern ostream which is standard output stream for errors and e.what() tells that the conversion has failed
}


return 0;
}

Output :
boost::bad_any_cast: failed conversion using boost::any_cast

Comments