//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;
std::vector<boost::any>vec;
std::string s1;
a = 4;
vec.push_back(a);
a.clear();
if (a.empty())
{
std::cout << "a is empty\n";
}
a = 'z';
vec.push_back(a);
a = std::string("hello world!");
vec.push_back(a);
a = 7.22;
vec.push_back(a);
a = true;
vec.push_back(a);
for (int i = 0;i < vec.size();i++)
{ const std::type_info& ti = vec[i].type();
s1 = ti.name();
if (s1 == "int")
{
std::cout<<boost::any_cast<int>(vec[i])<<"\n";
}
else if (s1 == "double")
{
std::cout << boost::any_cast<double>(vec[i])<<"\n";
}
else if (s1 == "char")
{
std::cout << boost::any_cast<char>(vec[i])<<"\n";
}
else if(s1=="bool")
{
std::cout << std::boolalpha<<boost::any_cast<bool>(vec[i])<<"\n";
}
else
{
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)
{
w.swap(x);
}
int main()
{
boost::any a = 1, b = 2.4;
swap(a, b);
std::cout <<"b = "<< boost::any_cast<int>(b)<<"\n";
std::cout <<"a = "<<boost::any_cast<double>(a)<<"\n";
return 0; }
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;
try {
std::cout << boost::any_cast<float>(b) << "\n";
}
catch (boost::bad_any_cast& e)
{
std::cerr << e.what() << "\n";
}
return 0;
}
Output :
boost::bad_any_cast: failed conversion using boost::any_cast
Comments