registry.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2013-2019 Antony Polukhin
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See the accompanying file LICENSE_1_0.txt
  4. // or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
  5. //[type_index_registry_example
  6. /*`
  7. The following example shows how an information about a type could be stored.
  8. Example works with and without RTTI.
  9. */
  10. #include <boost/type_index.hpp>
  11. #include <boost/unordered_set.hpp>
  12. //<-
  13. // Making `#include <cassert>` visible in docs, while actually using `BOOST_TEST`
  14. // instead of `assert`. This is required to verify correct behavior even if NDEBUG
  15. // is defined and to avoid `unused local variable` warnings with defined NDEBUG.
  16. #include <boost/core/lightweight_test.hpp>
  17. #ifdef assert
  18. # undef assert
  19. #endif
  20. #define assert(X) BOOST_TEST(X)
  21. /* !Comment block is not closed intentionaly!
  22. //->
  23. #include <cassert>
  24. //<-
  25. !Closing comment block! */
  26. //->
  27. int main() {
  28. boost::unordered_set<boost::typeindex::type_index> types;
  29. // Storing some `boost::type_info`s
  30. types.insert(boost::typeindex::type_id<int>());
  31. types.insert(boost::typeindex::type_id<float>());
  32. // `types` variable contains two `boost::type_index`es:
  33. assert(types.size() == 2);
  34. // Const, volatile and reference will be striped from the type:
  35. bool is_inserted = types.insert(boost::typeindex::type_id<const int>()).second;
  36. assert(!is_inserted);
  37. assert(types.erase(boost::typeindex::type_id<float&>()) == 1);
  38. // We have erased the `float` type, only `int` remains
  39. assert(*types.begin() == boost::typeindex::type_id<int>());
  40. //<-
  41. return boost::report_errors();
  42. //->
  43. }
  44. //] [/type_index_registry_example]