mirror of
https://github.com/boostorg/type_index.git
synced 2025-07-31 21:04:29 +02:00
Fixed issueas mentioned by Mathieu Champlon during Boost review
This commit is contained in:
@@ -11,10 +11,10 @@
|
||||
]
|
||||
|
||||
[section Motivation]
|
||||
Sometimes getting and storing information about a type at runtime is required. For such cases a construction like `&typeid(T)` or C++11 class `std::type_index` is usually used. And that is the point, where problems start:
|
||||
Sometimes getting and storing information about a type at runtime is required. For such cases a construction like `&typeid(T)` or C++11 class `std::type_index` is usually used, which is where problems start:
|
||||
|
||||
* `typeid(T)` and `std::type_index` require Run Time Type Info (RTTI)
|
||||
* some implementations of `typeid(T)` strip const, volatile and references from type, while others don't
|
||||
* some implementations of `typeid(T)` erroneously do not strip const, volatile and references from type
|
||||
* some compilers have bugs and do not correctly compare `std::type_info` objects across shared libraries
|
||||
* only a few implementations of Standard Library currently provide `std::type_index`
|
||||
* no easy way to store type info without stripping const, volatile and references
|
||||
@@ -31,7 +31,8 @@ Boost.TypeIndex library was designed to work around all those issues.
|
||||
|
||||
[section Getting started]
|
||||
|
||||
`boost::typeindex::type_info` is a drop-in replacement for `std::type_info` and `boost::typeindex::type_index`
|
||||
[classref boost::typeindex::type_info boost::typeindex::type_info] is a drop-in replacement for `std::type_info` and
|
||||
[classref boost::typeindex::type_index boost::typeindex::type_index]
|
||||
is a drop-in replacement for `std::type_index`. Unlike Standard Library versions those classes may work without RTTI.
|
||||
|
||||
`type_index` provides the full set of comparison operators, hashing functions and ostream
|
||||
@@ -42,43 +43,73 @@ operators, so it can be used with any container class.
|
||||
To start using Boost.TypeIndex:
|
||||
|
||||
[table:porting
|
||||
[[Replace this:][With the following:]]
|
||||
[[Replace this:][With the following:][More Info]]
|
||||
[[``
|
||||
#include <typeinfo>
|
||||
#include <typeindex>
|
||||
``][``
|
||||
#include <boost/type_index.hpp>
|
||||
``]]
|
||||
``][
|
||||
[headerref boost/type_index.hpp more... ]
|
||||
]]
|
||||
|
||||
[[``
|
||||
std::type_index
|
||||
``][``
|
||||
boost::typeindex::type_index
|
||||
``]]
|
||||
``][
|
||||
[classref boost::typeindex::type_index more... ]
|
||||
]]
|
||||
|
||||
[[``
|
||||
typeid(T)
|
||||
typeid(please_save_modifiers<T>)
|
||||
typeid(T).name() // not human readable
|
||||
typeid(variable)
|
||||
``][``
|
||||
boost::typeindex::type_id<T>()
|
||||
boost::typeindex::type_id_with_cvr<T>()
|
||||
boost::typeindex::type_id<T>().pretty_name() // human readable
|
||||
boost::typeindex::type_id_runtime(variable)
|
||||
``]]
|
||||
``][
|
||||
[funcref boost::typeindex::type_id more... ]
|
||||
|
||||
[classref boost::typeindex::type_index more... ]
|
||||
|
||||
[funcref boost::typeindex::type_id_runtime more... ]
|
||||
]]
|
||||
|
||||
[[``
|
||||
const std::type_info& v1 = typeid(int); // when reference to `std::type_info` is required
|
||||
const std::type_info* v2 = &typeid(int); // other cases
|
||||
// attempt to save const, volatile, reference
|
||||
typeid(please_save_modifiers<T>)
|
||||
``][``
|
||||
const boost::typeindex::type_info& v1 = boost::typeindex::type_id<int>().type_info();
|
||||
boost::typeindex::type_index v2 = boost::typeindex::type_id<int>();
|
||||
``]]
|
||||
// cvr = const, volatile, reference
|
||||
boost::typeindex::type_id_with_cvr<T>()
|
||||
``][
|
||||
[funcref boost::typeindex::type_id_with_cvr 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>();
|
||||
``][
|
||||
[classref boost::typeindex::type_index more... ]
|
||||
|
||||
[funcref boost::typeindex::type_id 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.
|
||||
If you are using [funcref boost::typeindex::type_id_runtime type_id_runtime()] methods
|
||||
and RTTI is disabled, make sure that classes that are passed to
|
||||
`type_id_runtime()` are marked with
|
||||
[macroref BOOST_TYPE_INDEX_REGISTER_CLASS BOOST_TYPE_INDEX_REGISTER_CLASS] macro.
|
||||
|
||||
[endsect]
|
||||
|
||||
@@ -171,13 +202,13 @@ public: // visitor interfaces
|
||||
[endsect]
|
||||
|
||||
[section How it works]
|
||||
`type_index` is just a typedef for `stl_type_index` or `ctti_type_index`.
|
||||
`type_index` is just a typedef for [classref boost::typeindex::stl_type_index]
|
||||
or [classref boost::typeindex::ctti_type_index].
|
||||
|
||||
Depending on the `typeid()` availability TypeIndex library will choose an optimal class for
|
||||
`type_index`. In cases when at least basic support for `typeid()` is available `boost::typeindex::stl_type_index`
|
||||
will be used.
|
||||
`type_index`. In cases when at least basic support for `typeid()` is available `stl_type_index` will be used.
|
||||
|
||||
`BOOST_TYPE_INDEX_REGISTER_CLASS` macro is a helper macro that places some virtual helper functions or
|
||||
[macroref BOOST_TYPE_INDEX_REGISTER_CLASS] macro is a helper macro that places some virtual helper functions or
|
||||
expands to nothing.
|
||||
|
||||
Issues with cross module type comparison on a bugged compilers are bypassed by directly comparing strings with type
|
||||
@@ -206,7 +237,7 @@ Issues with cross module type comparison on a bugged compilers are bypassed by d
|
||||
|
||||
[xinclude autodoc.xml]
|
||||
|
||||
[section Making own type_index]
|
||||
[section Making a custom type_index]
|
||||
|
||||
Sometimes there may be a need to create your own type info system. This may be usefull if you wish to store some more info about types (PODness, size of a type, pointers to common functions...) or if you have an idea of a more compact types representations.
|
||||
|
||||
|
Reference in New Issue
Block a user