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 to view this page for the latest version.
PrevUpHomeNext

Getting started

How to use
Example with Boost.Any
Example with Boost.Variant

boost::typeindex::type_info is a drop-in replacement for std::type_info and boost::typeindex::type_index is a drop-in replacement for std::type_index. Unlike Standard Library versions those classes can work without RTTI.

type_index provides the full set of comparison operators, hashing functions and ostream operators, so it can be used with any container class.

To start using Boost.TypeIndex:

Replace this:

With the following:

More Info

#include <typeinfo>
#include <typeindex>
#include <boost/type_index.hpp>

more...

std::type_index
boost::typeindex::type_index

more...

typeid(T)
typeid(T).name() // not human readable
typeid(variable)
boost::typeindex::type_id<T>()
boost::typeindex::type_id<T>().pretty_name() // human readable
boost::typeindex::type_id_runtime(variable)

more...

more...

more...

// attempt to save const, volatile, reference
typeid(please_save_modifiers<T>)
// cvr = const, volatile, reference
boost::typeindex::type_id_with_cvr<T>()

more...

// when reference to `std::type_info` is required
const std::type_info& v1 = typeid(int);

// other cases
const std::type_info* v2 = &typeid(int);
const boost::typeindex::type_info& v1
    = boost::typeindex::type_id<int>().type_info();

boost::typeindex::type_index v2
    = boost::typeindex::type_id<int>();

more...

more...

If you are using type_id_runtime() methods and RTTI is disabled, make sure that classes that are passed to type_id_runtime() are marked with BOOST_TYPE_INDEX_REGISTER_CLASS macro.

Here is how TypeIndex could be used in boost/any.hpp:

Before

With TypeIndex

#include <typeinfo>
#include <boost/type_index.hpp>
virtual const std::type_info & type() const BOOST_NOEXCEPT
{
    // requires RTTI
    return typeid(ValueType);
}
virtual const boost::typeindex::type_info & type() const BOOST_NOEXCEPT
{
    // now works even with RTTI disabled
    return boost::typeindex::type_id<ValueType>().type_info();
}

Here is how TypeIndex could be used in boost/variant/variant.hpp:

Before

With TypeIndex

#if !defined(BOOST_NO_TYPEID)
#include <typeinfo> // for typeid, std::type_info
#endif // BOOST_NO_TYPEID
#include <boost/type_index.hpp>
#if !defined(BOOST_NO_TYPEID)

class reflect
    : public static_visitor<const std::type_info&>
{
public: // visitor interfaces

    template <typename T>
    const std::type_info& operator()(const T&) const BOOST_NOEXCEPT
    {
        return typeid(T);
    }

};

#endif // BOOST_NO_TYPEID
class reflect
    : public static_visitor<const boost::typeindex::type_info&>
{
public: // visitor interfaces

    template <typename T>
    const boost::typeindex::type_info& operator()(const T&) const BOOST_NOEXCEPT
    {
        return boost::typeindex::type_id<T>().type_info();
    }

};
#if !defined(BOOST_NO_TYPEID)
    const std::type_info& type() const
    {
        detail::variant::reflect visitor;
        return this->apply_visitor(visitor);
    }
#endif
const boost::typeindex::type_info& type() const
{
    detail::variant::reflect visitor;
    return this->apply_visitor(visitor);
}

PrevUpHomeNext