// Copyright (c) 2018-2025 Jean-Louis Leroy // Distributed under the Boost Software License, Version 1.0. // See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #ifdef _MSC_VER #pragma warning(disable : 4312) #endif struct Animal { Animal(unsigned type) : type(type) { } virtual ~Animal() = default; unsigned type; static constexpr unsigned static_type = 1; }; struct Cat : Animal { Cat() : Animal(static_type) { } static constexpr unsigned static_type = 2; }; struct Dog : Animal { Dog() : Animal(static_type) { } static constexpr unsigned static_type = 3; }; #include #include // tag::policy[] namespace bom = boost::openmethod; struct custom_rtti : bom::policies::rtti { template struct fn : bom::policies::rtti::defaults { template static constexpr bool is_polymorphic = std::is_base_of_v; template static auto static_type() -> bom::type_id { if constexpr (is_polymorphic) { return reinterpret_cast(T::static_type); } else { return nullptr; } } template static auto dynamic_type(const T& obj) -> bom::type_id { if constexpr (is_polymorphic) { return reinterpret_cast(obj.type); } else { return nullptr; } } }; }; // end::policy[] // tag::registry[] struct custom_registry : bom::registry {}; #define BOOST_OPENMETHOD_DEFAULT_REGISTRY custom_registry // end::registry[] // tag::example[] #include #include #include using boost::openmethod::virtual_ptr; BOOST_OPENMETHOD(poke, (std::ostream&, virtual_ptr), void); BOOST_OPENMETHOD_OVERRIDE( poke, (std::ostream & os, virtual_ptr /*cat*/), void) { os << "hiss"; } BOOST_OPENMETHOD_OVERRIDE( poke, (std::ostream & os, virtual_ptr /*dog*/), void) { os << "bark"; } BOOST_OPENMETHOD_CLASSES(Animal, Cat, Dog); auto main() -> int { boost::openmethod::initialize(); std::unique_ptr a(new Cat); std::unique_ptr b(new Dog); poke(std::cout, *a); // prints "hiss" std::cout << "\n"; poke(std::cout, *b); // prints "bark" std::cout << "\n"; return 0; } // end::example[]