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++.

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.


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 &lt;iostream&gt;<br />
#include &quot;boost/any.hpp&quot;</p>
<p>int main()<br />
{<br />
   boost::any var1 = 5;<br />
   std::cout &lt;&lt; boost::any_cast&lt;int&gt;(var1) &lt;&lt;std::endl;<br />
   int i = boost::any_cast&lt;int&gt;(var1);<br />
   std::cout &lt;&lt; i &lt;&lt;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 &lt;iostream&gt;<br />
#include &quot;boost/any.hpp&quot;</p>
<p>int main()<br />
{<br />
   try<br />
   {<br />
        boost::any var1 = 5;<br />
        std::cout &lt;&lt; boost::any_cast&lt;std::string&gt;(var1) &lt;&lt;std::endl;<br />
   }<br />
   catch(boost::bad_any_cast &amp;e)<br />
   {<br />
       std::cout &lt;&lt; e.what() &lt;&lt; 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 &lt;iostream&gt;<br />
#include &lt;string&gt;<br />
#include &quot;boost/any.hpp&quot;</p>
<p>int main()<br />
{<br />
    boost::any var1 = 5;<br />
    std::string * str = boost::any_cast&lt;std::string&gt;(&amp;var1); // It will return NULL<br />
    if(str == NULL)<br />
        std::cout &lt;&lt; &quot;Wrong Typecast dude :-)&quot; &lt;&lt; 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 &lt;iostream&gt;<br />
#include &lt;string&gt;<br />
#include &quot;boost/any.hpp&quot;</p>
<p>int main()<br />
{<br />
    boost::any var1 = 5;<br />
    int * ptr = boost::any_cast&lt;int&gt;(&amp;var1);<br />
    if(ptr != NULL)<br />
        std::cout &lt;&lt; &quot;Right Typecast to int&quot; &lt;&lt; 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&lt;&lt;&quot;var is empty&quot;&lt;&lt;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 = &amp;k;<br />
const std::type_info &amp; typeInfo = var.type();<br />
std::cout&lt;&lt;typeInfo.name() &lt;&lt; std::endl;<br />

When not to use boost::any :

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

Join a list of 2000+ Programmers for latest Tips & Tutorials