blob: 3da75271e81a45d35a379c9139045f99ebef1c53 [file] [log] [blame]
Brian Silverman87d3c952018-08-05 00:08:45 -07001// Copyright 2013-2014 Antony Polukhin
2
3// Distributed under the Boost Software License, Version 1.0.
4// (See the accompanying file LICENSE_1_0.txt
5// or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
6
7//[type_index_derived_example
8/*`
9 The following example shows that `type_info` is able to store the real type, successfully getting through
10 all the inheritances.
11
12 Example works with and without RTTI."
13*/
14
15#include <boost/type_index.hpp>
16#include <boost/type_index/runtime_cast/register_runtime_class.hpp>
17#include <iostream>
18
19struct A {
20 BOOST_TYPE_INDEX_REGISTER_CLASS
21 virtual ~A(){}
22};
23struct B: public A { BOOST_TYPE_INDEX_REGISTER_CLASS };
24struct C: public B { BOOST_TYPE_INDEX_REGISTER_CLASS };
25struct D: public C { BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS) };
26
27void print_real_type(const A& a) {
28 std::cout << boost::typeindex::type_id_runtime(a).pretty_name() << '\n';
29}
30
31int main() {
32 C c;
33 const A& c_as_a = c;
34 print_real_type(c_as_a); // Outputs `struct C`
35 print_real_type(B()); // Outputs `struct B`
36
37/*`
38 It's also possible to use type_id_runtime with the BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS, which adds additional
39 information for runtime_cast to work.
40*/
41 D d;
42 const A& d_as_a = d;
43 print_real_type(d_as_a); // Outputs `struct D`
44
45}
46
47//] [/type_index_derived_example]