Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Function any_cast

boost::type_erasure::any_cast

Synopsis

// In header: <boost/type_erasure/any_cast.hpp>


template<typename T, typename Concept, typename Tag> 
  T any_cast(any< Concept, Tag > & arg);
template<typename T, typename Concept, typename Tag> 
  T any_cast(const any< Concept, Tag > & arg);
template<typename T, typename Concept, typename Tag> 
  T any_cast(any< Concept, Tag > * arg);
template<typename T, typename Concept, typename Tag> 
  T any_cast(const any< Concept, Tag > * arg);

Description

Attempts to extract the object that arg holds. If casting to a pointer fails, any_cast returns a null pointer. Casting to void* always succeeds and returns the address of stored object.

any<mpl::vector<typeid_<>, copy_constructible<> > > x(1);
any_cast<int>(x);      // returns 1
any_cast<int&>(x);     // returns a reference to the contents of x
any_cast<double>(x);   // throws bad_any_cast
any_cast<int*>(&x);    // returns a pointer to the contents of x
any_cast<void*>(&x);   // returns a pointer to the contents of x
any_cast<double*>(&x); // returns NULL

Requires:

if arg is a pointer, T must be a pointer type.

Concept must contain typeid_<Tag>.

Throws:

bad_any_cast if arg doesn't contain an object of type T and we're casting to a value or reference.

PrevUpHomeNext