Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2005-02-25 11:48:09


Jody Hagins wrote:
>
> The library, as used in Boost.Python, relies on calling the no-parameter
> template function, type_id, to get the type info object. Use of
> typeid() by itself is absent, because of the problem with compilers
> getting the typeid() implementation correct. For example:
>
> struct foo { /* ... */ };
> boost::python::type_info ti = boost::python::type_id< foo >();
>
> Unfortunately, boost::python::type_id<>() has a couple major drawbacks
> (and a number of "minor" ones).
>
> 1. It only supports type-id calls, with a specific type passed as a
> template parameter. The ability to compute type_info based on the
> result of an expression is scant (a bit more functionality can possibly
> be obtained with more tricks like the ingenious typeof() stuff recently
> posted).
>

Please have a look at the article just published at the C++ Source
(http://www.artima.com/cppsource/index.jsp) entitled "Conditional Love".
If you want a BOOST_TYPEID macro that accepts an expression and returns
the type_info without evaluating the expression, it's really quite simple:

template<typename T>
T const * encode_type( T const & ) {
     return 0;
}

template<typename T>
type_info type_id_helper( T const * ) {
     // return type_info for type T
}

#define BOOST_TYPEID( expr ) \
     type_id_helper( true? 0 : encode_type( expr ) )

With carefully selected overloads of encode_type and type_id_helper, you
can correctly handle cv-qualifier and rvalues/lvalues.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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