Boost logo

Boost :

Subject: [boost] [any] boost::get style accessors
From: Christoph Heindl (christoph.heindl_at_[hidden])
Date: 2010-04-27 13:15:09


Hi all,

I couldn't find any boost::get<T> style accessors for boost::any, so I
came up with an implementation that looks something along the
following lines.

#include <boost/any.hpp>
#include <boost/call_traits.hpp>

namespace boost {

  template<class T>
  typename boost::call_traits<T>::reference get(any &a) {
    return boost::any_cast<typename boost::call_traits<T>::reference>(a);
  }

  template<class T>
  typename boost::call_traits<T>::const_reference get(const any &a) {
    return boost::any_cast<typename boost::call_traits<T>::const_reference>(a);
  }
}

In case it cannot be converted to the desired destination type a
bad_any_cast exception is thrown.

The above code enables boost.any and boost.variant to be used
interchangeably as far as boost::get<T> is concerned.

Any thoughts?

Best regards,
Christoph


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk