Brian Silverman | 87d3c95 | 2018-08-05 00:08:45 -0700 | [diff] [blame^] | 1 | // Copyright 2013-2017 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_registry_example |
| 8 | /*` |
| 9 | The following example shows how an information about a type could be stored. |
| 10 | Example works with and without RTTI. |
| 11 | */ |
| 12 | |
| 13 | #include <boost/type_index.hpp> |
| 14 | #include <boost/unordered_set.hpp> |
| 15 | //<- |
| 16 | // Making `#include <cassert>` visible in docs, while actually using `BOOST_TEST` |
| 17 | // instead of `assert`. This is required to verify correct behavior even if NDEBUG |
| 18 | // is defined and to avoid `unused local variable` warnings with defined NDEBUG. |
| 19 | #include <boost/core/lightweight_test.hpp> |
| 20 | #ifdef assert |
| 21 | # undef assert |
| 22 | #endif |
| 23 | #define assert(X) BOOST_TEST(X) |
| 24 | /* !Comment block is not closed intentionaly! |
| 25 | //-> |
| 26 | #include <cassert> |
| 27 | //<- |
| 28 | !Closing comment block! */ |
| 29 | //-> |
| 30 | |
| 31 | int main() { |
| 32 | boost::unordered_set<boost::typeindex::type_index> types; |
| 33 | |
| 34 | // Storing some `boost::type_info`s |
| 35 | types.insert(boost::typeindex::type_id<int>()); |
| 36 | types.insert(boost::typeindex::type_id<float>()); |
| 37 | |
| 38 | // `types` variable contains two `boost::type_index`es: |
| 39 | assert(types.size() == 2); |
| 40 | |
| 41 | // Const, volatile and reference will be striped from the type: |
| 42 | bool is_inserted = types.insert(boost::typeindex::type_id<const int>()).second; |
| 43 | assert(!is_inserted); |
| 44 | assert(types.erase(boost::typeindex::type_id<float&>()) == 1); |
| 45 | |
| 46 | // We have erased the `float` type, only `int` remains |
| 47 | assert(*types.begin() == boost::typeindex::type_id<int>()); |
| 48 | } |
| 49 | |
| 50 | //] [/type_index_registry_example] |