title: Cast boost::any instance to its real type
body: <p>I recently started to use <code>Boost</code> C++ library and I am testing the <code>any</code> class which can hold any data type. Actually I am trying to define the <code>operator&lt;&lt;</code> to print easily the content of any variable of type <code>any</code> (and sure, the class of the content should have the <code>operator&lt;&lt;</code> defined too).
  I only started by sample types ( <code>int</code>, <code>double</code> ...) because they have be displayed by default. And till now, I have this code :</p>
  
  <pre><code>#include &lt;boost/any.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  using namespace boost;
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, any&amp; a){
      if(a.type() == typeid(int))
          out &lt;&lt; any_cast&lt;int&gt;(a);
      else if(a.type() == typeid(double))
          out &lt;&lt; any_cast&lt;double&gt;(a);
      // else ...
      // But what about other types/classes ?!
  }
  
  int main(){
      any a = 5;
      cout &lt;&lt; a &lt;&lt; endl;
  }
  </code></pre>
  
  <p>So the problem here is that I have to enumerate all possible types. Is there any way to cast the variable to a <code>particular type</code> having the <code>type_info</code> of this <code>particular type</code> ?</p>