We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

What is boost::any  :


boost::any is a class from Boost::any Library. It provides the ability to store arbitrary information in a variable in C++.

Advertisements

What to include :

Header File – “boost/any.hpp”.

What can be stored in boost::any :

Anything that is copy constructable like,

  • Built-in data types i.e. int, char, bool and std::string etc.
  • User defined data types that has copy constructor.

[showads ad=inside_post]
How to convert back from boost::any to other type :

To convert boost::any to any other type boost::any_cast is used.

<br />
#include <iostream><br />
#include "boost/any.hpp"</p>
<p>int main()<br />
{<br />
   boost::any var1 = 5;<br />
   std::cout << boost::any_cast<int>(var1) <<std::endl;<br />
   int i = boost::any_cast<int>(var1);<br />
   std::cout << i <<std::endl;<br />
   return 0;<br />
}<br />
Output:

5
5

What if casted into wrong type :

It will cause boost::bad_any_cast exception,
For Example,

<br />
#include <iostream><br />
#include "boost/any.hpp"</p>
<p>int main()<br />
{<br />
   try<br />
   {<br />
        boost::any var1 = 5;<br />
        std::cout << boost::any_cast<std::string>(var1) <<std::endl;<br />
   }<br />
   catch(boost::bad_any_cast &e)<br />
   {<br />
       std::cout << e.what() << std::endl;<br />
   }<br />
   return 0;<br />
}<br />
Output:

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

How to avoid boost::bad_any_cast but still check wrong typecasting :

<br />
#include <iostream><br />
#include <string><br />
#include "boost/any.hpp"</p>
<p>int main()<br />
{<br />
    boost::any var1 = 5;<br />
    std::string * str = boost::any_cast<std::string>(&var1); // It will return NULL<br />
    if(str == NULL)<br />
        std::cout << "Wrong Typecast dude :-)" << std::endl;<br />
    return 0;<br />
}<br />
Output:

Wrong Typecast dude :-)

If actual value of variable inside boost::any is not matched with casting parameter then it will return error.

<br />
#include <iostream><br />
#include <string><br />
#include "boost/any.hpp"</p>
<p>int main()<br />
{<br />
    boost::any var1 = 5;<br />
    int * ptr = boost::any_cast<int>(&var1);<br />
    if(ptr != NULL)<br />
        std::cout << "Right Typecast to int" << std::endl;<br />
    return 0;<br />
}<br />
Output:

Right Typecast to int.

Useful member functions :

1.)  empty():

Tells if variable is empty or not. Return type is bool.

<br />
boost::any var;<br />
if(var.empty())<br />
	std::cout<<"var is empty"<<std::endl;<br />

2.)  type():

Returns the std::type_info, that cab be used to fetch the type information of the value it contains.

<br />
int k = 8;<br />
boost::any var = &k;<br />
const std::type_info & typeInfo = var.type();<br />
std::cout<<typeInfo.name() << std::endl;<br />

When not to use boost::any :

In time critical code because it internally uses RTTI, whis is slow.

Cart

Your Cart is Empty

Back To Shop