# Boost.TypeIndex Header Reference

Includes minimal set of headers required to use the Boost.TypeIndex library.

By inclusion of this file most optimal type index classes will be included and used as a boost::typeindex::type_index and boost::typeindex::type_info.

Depending on a compiler flags, optimal implementation of type_index will be used as a default boost::typeindex::type_index.

Could be a `boost::typeindex::stl_type_index`, `boost::typeindex::ctti_type_index` or user defined type_index class.

See `boost::typeindex::type_index_facade` for a full description of type_index functions.

platform_specific

Depending on a compiler flags, optimal implementation of type_info will be used as a default boost::typeindex::type_info.

Could be a std::type_info, `boost::typeindex::detail::ctti_data` or some user defined class.

type_info is not copyable or default constructible. It is not assignable too!

type_index::type_info_t
type_index

Type for which type_index must be created.

Function to get boost::typeindex::type_index for a type T. Removes const, volatile && and & modifiers from T.

Example:

    type_index ti = type_id<int&>();
    std::cout << ti.pretty_name();  // Outputs 'int'

boost::typeindex::type_index with information about the specified type T.

`Nothing.`

type_index

Type for which type_index must be created.

Function for constructing boost::typeindex::type_index instance for type T. Does not remove const, volatile, & and && modifiers from T.

If T has no const, volatile, & and && modifiers, then returns exactly the same result as in case of calling `type_id<T>()`.

Example:

    type_index ti = type_id_with_cvr<int&>();
    std::cout << ti.pretty_name();  // Outputs 'int&'

boost::typeindex::type_index with information about the specified type T.

`Nothing.`

type_index `const T &

Variable which runtime type must be returned.

`

Function that works exactly like C++ typeid(rtti_val) call, but returns boost::type_index.

Returns runtime information about specified type.

Requirements: RTTI available or Base and Derived classes must be marked with BOOST_TYPE_INDEX_REGISTER_CLASS.

Example:

    struct Base { virtual ~Base(){} };
    struct Derived: public Base  {};
    ...
    Derived d;
    Base& b = d;
    type_index ti = type_id_runtime(b);
    std::cout << ti.pretty_name();  // Outputs 'Derived'

boost::typeindex::type_index with information about the specified variable.

`Nothing.`

BOOST_TYPE_INDEX_REGISTER_CLASS is used to help to emulate RTTI. Put this macro into the public section of polymorphic class to allow runtime type detection.

Depending on the typeid() availability this macro will expand to nothing or to virtual helper function `virtual const type_info& boost_type_info_type_id_runtime_() const noexcept`.

Example:

    class A {
    public:
        BOOST_TYPE_INDEX_REGISTER_CLASS
        virtual ~A(){}
    };

    struct B: public A {
        BOOST_TYPE_INDEX_REGISTER_CLASS
    };

    struct C: public B {
        BOOST_TYPE_INDEX_REGISTER_CLASS
    };

    ...

    C c1;
    A* pc1 = &c1;
    assert(boost::typeindex::type_id<C>() == boost::typeindex::type_id_runtime(*pc1));

BOOST_TYPE_INDEX_FUNCTION_SIGNATURE is used by `boost::typeindex::ctti_type_index` class to deduce the name of a type. If your compiler is not recognized by the TypeIndex library and you wish to work with `boost::typeindex::ctti_type_index`, you may define this macro by yourself.

BOOST_TYPE_INDEX_FUNCTION_SIGNATURE must be defined to a compiler specific macro that outputs the whole function signature including template parameters.

If your compiler is not recognised and BOOST_TYPE_INDEX_FUNCTION_SIGNATURE is not defined, then a compile-time error will arise at any attempt to use `boost::typeindex::ctti_type_index` classes.

See BOOST_TYPE_INDEX_REGISTER_CTTI_PARSING_PARAMS and BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING for an information of how to tune the implementation to make a nice pretty_name() output.

This is a helper macro for making correct pretty_names() with RTTI off.

BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING macro may be defined to '(begin_skip, end_skip, runtime_skip, runtime_skip_until)' with parameters for adding a support for compilers, that by default are not recognized by TypeIndex library.

Example:

Imagine the situation when

    boost::typeindex::ctti_type_index::type_id<int>().pretty_name()

returns the following string:

    "static const char *boost::detail::ctti<int>::n() [T = int]"

and

    boost::typeindex::ctti_type_index::type_id<short>().pretty_name()

returns the following:

    "static const char *boost::detail::ctti<short>::n() [T = short]"

As we may see first 39 characters are "static const char *boost::detail::ctti<" and they do not depend on the type T. After first 39 characters we have a human readable type name which is duplicated at the end of a string. String always ends on ']', which consumes 1 character.

Now if we define `BOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING` to `(39, 1, false, "")` we'll be getting

    "int>::n() [T = int"

for `boost::typeindex::ctti_type_index::type_id<int>().pretty_name()` and

    "short>::n() [T = short"

for `boost::typeindex::ctti_type_index::type_id<short>().pretty_name()`.

Now we need to take additional care of the characters that go before the last mention of our type. We'll do that by telling the macro that we need to cut off everything that goes before the "T = " including the "T = " itself:

    (39, 1, true, "T = ")

In case of GCC or Clang command line we need to add the following line while compiling all the sources:

    -DBOOST_TYPE_INDEX_CTTI_USER_DEFINED_PARSING='(39, 1, true, "T = ")'

See RTTI emulation limitations for more info.

BOOST_TYPE_INDEX_USER_TYPEINDEX can be defined to the path to header file with user provided implementation of type_index.

See Making a custom type_index section of documentation for usage example.

BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY is a helper macro that must be defined if mixing RTTI on/off modules. See Mixing sources with RTTI on and RTTI off section of documentation for more info.

Contains `boost::typeindex::ctti_type_index` class that is constexpr if C++14 constexpr is supported by compiler.

`boost::typeindex::ctti_type_index` class can be used as a drop-in replacement for std::type_index.

It is used in situations when typeid() method is not available or BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY macro is defined.

This class is a wrapper that pretends to work exactly like `stl_type_index`, but does not require RTTI support. For description of functions see `type_index_facade`.

This class on C++14 compatible compilers has following functions marked as constexpr:

- default constructor

- copy constructors and assignemnt operations

- class methods: name(), before(const ctti_type_index& rhs), equal(const ctti_type_index& rhs)

- static methods type_id<T>(), type_id_with_cvr<T>()

- comparison operators

This class produces slightly longer type names, so consider using `stl_type_index` in situations when typeid() is working.

unspecified
std::size_t
const char *
const `type_info_t` &
constexpr const char *
constexpr const char *
std::string
std::size_t
constexpr bool `const `ctti_type_index` &`
constexpr bool `const `ctti_type_index` &`
const `type_info_t` &
constexpr `ctti_type_index`
constexpr `ctti_type_index`
`ctti_type_index` `const T &`
unspecified Helper method for getting `detail::ctti_data` of a template parameter T.

Contains the basic utilities necessary to fully emulate dynamic_cast for language level constructs (raw pointers and references).

boost::typeindex::runtime_cast is a drop in replacement for dynamic_cast that can be used in situations where traditional rtti is either unavailable or undesirable.

Contains the overload of boost::typeindex::runtime_pointer_cast for `boost::shared_ptr` types.

`boost::shared_ptr`< T >

The desired target type to return a pointer of.

A complete class type of the source instance pointed to from u.

boost::shared_ptr
< U > const &
Creates a new instance of std::shared_ptr whose stored pointer is obtained from u's stored pointer using a runtime_cast.

The new `shared_ptr` will share ownership with u, except that it is empty if the runtime_cast performed by runtime_pointer_cast returns a null pointer.

If there exists a valid conversion from U* to T*, returns a boost::shared_ptr<T> that points to an address suitably offset from u. If no such conversion exists, returns boost::shared_ptr<T>();

T

The desired target type. Like dynamic_cast, must be a pointer to complete class type.

A complete class type of the source instance, u.

U *
Safely converts pointers to classes up, down, and sideways along the inheritance hierarchy.

If there exists a valid conversion from U* to T, returns a T that points to an address suitably offset from u. If no such conversion exists, returns NULL.

T

The desired target type. Like dynamic_cast, must be a pointer to complete class type.

A complete class type of the source instance, u.

U const *
Safely converts pointers to classes up, down, and sideways along the inheritance hierarchy.

If there exists a valid conversion from U* to T, returns a T that points to an address suitably offset from u. If no such conversion exists, returns NULL.

T *

The desired target type to return a pointer to.

A complete class type of the source instance, u.

U *
Safely converts pointers to classes up, down, and sideways along the inheritance hierarchy.

If there exists a valid conversion from U const* to T*, returns a T* that points to an address suitably offset from u. If no such conversion exists, returns NULL.

T const *

The desired target type to return a pointer to.

A complete class type of the source instance, u.

U const *
Safely converts pointers to classes up, down, and sideways along the inheritance hierarchy.

If there exists a valid conversion from U const* to T const*, returns a T const* that points to an address suitably offset from u. If no such conversion exists, returns NULL.

Contains the overload of boost::typeindex::runtime_cast for reference types.

exception
Indicates that runtime_cast was unable to perform the desired cast operation because the source instance was not also an instance of the target type.
boost::add_reference< T >::type

The desired target type. Like dynamic_cast, must be a pointer to complete class type.

A complete class type of the source instance, u.

U &
Safely converts references to classes up, down, and sideways along the inheritance hierarchy.

If there exists a valid conversion from U& to T, returns a T that references an address suitably offset from u. If no such conversion exists, throws `boost::typeindex::bad_runtime_cast`.

boost::add_reference< const T >::type

The desired target type. Like dynamic_cast, must be a pointer to complete class type.

A complete class type of the source instance, u.

U const &
Safely converts references to classes up, down, and sideways along the inheritance hierarchy.

If there exists a valid conversion from U const& to T const, returns a T const that references an address suitably offset from u. If no such conversion exists, throws `boost::typeindex::bad_runtime_cast`.

Contains the macros BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST and BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS.

A Boost.Preprocessor sequence of the current class' direct bases, or BOOST_TYPE_INDEX_NO_BASE_CLASS if this class has no direct base classes.

Macro used to make a class compatible with boost::typeindex::runtime_cast.

BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS generates a virtual function in the current class that, when combined with the supplied base class information, allows boost::typeindex::runtime_cast to accurately convert between dynamic types of instances of the current class.

BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS also adds support for boost::typeindex::type_id_runtime by including BOOST_TYPE_INDEX_REGISTER_CLASS. It is typical that these features are used together, but in the event that BOOST_TYPE_INDEX_REGISTER_CLASS is undesirable in the current class, BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST is provided.

Example:

    struct base1 {
        BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS)
        virtual ~base1();
    };

    struct base2 {
        BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS)
        virtual ~base2();
    };

    struct derived1 : base1 {
        BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((base1))
    };

    struct derived2 : base1, base2 {
        BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((base1)(base2))
    };

    ...

    base1* pb1 = get_object();
    if(derived2* pb2 = boost::typeindex::runtime_cast<derived2*>(pb1)) {
        assert(boost::typeindex::type_id_runtime(*pb1)) == boost::typeindex::type_id<derived2>());
    }

A Boost.Preprocessor sequence of the current class' direct bases, or BOOST_TYPE_INDEX_NO_BASE_CLASS if this class has no direct base classes.

Macro used to make a class compatible with boost::typeindex::runtime_cast without including support for boost::typeindex::type_id_runtime.

BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST is provided as an alternative to BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS in the event that support for boost::typeindex::type_id_runtime is undesirable.

Example:

    struct base1 {
        BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST(BOOST_TYPE_INDEX_NO_BASE_CLASS)
        virtual ~base1();
    };

    struct base2 {
        BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST(BOOST_TYPE_INDEX_NO_BASE_CLASS)
        virtual ~base2();
    };

    struct derived1 : base1 {
        BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST((base1))
    };

    struct derived2 : base1, base2 {
        BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST((base1)(base2))
    };

    ...

    base1* pb1 = get_object();
    if(derived2* pb2 = boost::typeindex::runtime_cast<derived2*>(pb1))
    { /* can't call boost::typeindex::type_id_runtime(*pb1) here */ }

Instructs BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS and BOOST_TYPE_INDEX_IMPLEMENT_RUNTIME_CAST that this class has no base classes.

Contains the overload of boost::typeindex::runtime_pointer_cast for std::shared_ptr types.

std::shared_ptr< T >

The desired target type to return a pointer of.

A complete class type of the source instance pointed to from u.

std::shared_ptr< U > const &
Creates a new instance of std::shared_ptr whose stored pointer is obtained from u's stored pointer using a runtime_cast.

The new `shared_ptr` will share ownership with u, except that it is empty if the runtime_cast performed by runtime_pointer_cast returns a null pointer.

If there exists a valid conversion from U* to T*, returns a std::shared_ptr<T> that points to an address suitably offset from u. If no such conversion exists, returns std::shared_ptr<T>();

Contains `boost::typeindex::stl_type_index` class.

`boost::typeindex::stl_type_index` class can be used as a drop-in replacement for std::type_index.

It is used in situations when RTTI is enabled or typeid() method is available. When typeid() is disabled or BOOST_TYPE_INDEX_FORCE_NO_RTTI_COMPATIBILITY macro is defined boost::typeindex::ctti is usually used instead of `boost::typeindex::stl_type_index`.

boost::typeindex::type_index_facade< stl_type_index, std::type_info >

This class is a wrapper around std::type_info, that workarounds issues and provides much more rich interface. For description of functions see `type_index_facade`.

This class requires typeid() to work. For cases when RTTI is disabled see `ctti_type_index`.

std::type_info
const type_info_t &
const char *
const char *
std::string
std::size_t
bool `const `stl_type_index` &`
bool `const `stl_type_index` &`
const type_info_t &
`stl_type_index`
`stl_type_index`
`stl_type_index` `const T &`

Class derived from `type_index_facade`.

Class that will be used as a base type_info class.

This class takes care about the comparison operators, hash functions and ostream operators. Use this class as a public base class for defining new type_info-conforming classes.

Example:

    class stl_type_index: public type_index_facade<stl_type_index, std::type_info>
    {
    public:
        typedef std::type_info type_info_t;
    private:
        const type_info_t* data_;

    public:
        stl_type_index(const type_info_t& data) noexcept
            : data_(&data)
        {}
    // ...
    };

Take a look at the protected methods. They are not defined in `type_index_facade`. Protected member functions raw_name() must be defined in Derived class. All the other methods are mandatory.

See Also:

'Making a custom type_index' section for more information about creating your own type_index using `type_index_facade`.

TypeInfo
const char *

Override: This function may be redefined in Derived class. Overrides must not throw.

Name of a type. By default returns Derived::raw_name().

std::string

Override: This function may be redefined in Derived class. Overrides may throw.

Human readable type name. By default returns Derived::name().

bool `const Derived &`

Override: This function may be redefined in Derived class. Overrides must not throw.

True if two types are equal. By default compares types by raw_name().

bool `const Derived &`

Override: This function may be redefined in Derived class. Overrides must not throw.

True if rhs is greater than this. By default compares types by raw_name().

std::size_t

Override: This function may be redefined in Derived class. Overrides must not throw.

Derived class header must include <boost/container_hash/hash.hpp>, unless this function is redefined in Derived class to not use boost::hash_range().

Hash code of a type. By default hashes types by raw_name().

const char *

Override: This function must be redefined in Derived class. Overrides must not throw.

Pointer to unredable/raw type name.

const type_info_t &

Override: This function may be redefined in Derived class. Overrides must not throw.

Const reference to underlying low level type_info_t.

Derived

Type for which type_index must be created.

This is a factory method that is used to create instances of Derived classes. boost::typeindex::type_id() will call this method, if Derived has same type as boost::typeindex::type_index.

Override: This function may be redefined and made public in Derived class. Overrides must not throw. Overrides must remove const, volatile && and & modifiers from T.

type_index for type T.

Derived

Type for which type_index must be created.

This is a factory method that is used to create instances of Derived classes. boost::typeindex::type_id_with_cvr() will call this method, if Derived has same type as boost::typeindex::type_index.

Override: This function may be redefined and made public in Derived class. Overrides must not throw. Overrides must not remove const, volatile && and & modifiers from T.

type_index for type T.

Derived `const T &

Variable which runtime type will be stored in type_index.

`

This is a factory method that is used to create instances of Derived classes. boost::typeindex::type_id_runtime(const T&) will call this method, if Derived has same type as boost::typeindex::type_index.

Override: This function may be redefined and made public in Derived class.

type_index with runtime type of variable.

bool `const `type_index_facade` &` `const `type_index_facade` &` noexcept comparison operators for `type_index_facade` classes.
bool `const `type_index_facade` &` `const TypeInfo &` noexcept comparison operators for `type_index_facade` and it's TypeInfo classes.
bool `const TypeInfo &` `const `type_index_facade` &` noexcept comparison operators for `type_index_facade`'s TypeInfo and `type_index_facade` classes.
std::basic_ostream< CharT, TriatT > & `std::basic_ostream< CharT, TriatT > &` `const `type_index_facade`< Derived, TypeInfo > &` Ostream operator that will output demangled name.
std::size_t `const `type_index_facade`< Derived, TypeInfo > &`

This free function is used by Boost's unordered containers.

<boost/container_hash/hash.hpp> has to be included if this function is used.