| 1 | // Copyright Louis Dionne 2013-2022 |
|---|---|
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | #include <boost/hana/core/default.hpp> |
| 6 | #include <boost/hana/core/tag_of.hpp> |
| 7 | #include <boost/hana/integral_constant.hpp> |
| 8 | |
| 9 | #include <string> |
| 10 | #include <type_traits> |
| 11 | namespace hana = boost::hana; |
| 12 | |
| 13 | |
| 14 | namespace with_special_base_class { |
| 15 | //! [special_base_class] |
| 16 | struct special_base_class { }; |
| 17 | |
| 18 | template <typename T> |
| 19 | struct print_impl : special_base_class { |
| 20 | template <typename ...Args> |
| 21 | static constexpr auto apply(Args&& ...) = delete; |
| 22 | }; |
| 23 | |
| 24 | template <typename T> |
| 25 | struct Printable |
| 26 | : hana::integral_constant<bool, |
| 27 | !std::is_base_of<special_base_class, print_impl<hana::tag_of_t<T>>>::value |
| 28 | > |
| 29 | { }; |
| 30 | //! [special_base_class] |
| 31 | |
| 32 | //! [special_base_class_customize] |
| 33 | struct Person { std::string name; }; |
| 34 | |
| 35 | template <> |
| 36 | struct print_impl<Person> /* don't inherit from special_base_class */ { |
| 37 | // ... implementation ... |
| 38 | }; |
| 39 | |
| 40 | static_assert(Printable<Person>::value, ""); |
| 41 | static_assert(!Printable<void>::value, ""); |
| 42 | //! [special_base_class_customize] |
| 43 | } |
| 44 | |
| 45 | namespace actual { |
| 46 | //! [actual] |
| 47 | template <typename T> |
| 48 | struct print_impl : hana::default_ { |
| 49 | template <typename ...Args> |
| 50 | static constexpr auto apply(Args&& ...) = delete; |
| 51 | }; |
| 52 | |
| 53 | template <typename T> |
| 54 | struct Printable |
| 55 | : hana::integral_constant<bool, |
| 56 | !hana::is_default<print_impl<hana::tag_of_t<T>>>::value |
| 57 | > |
| 58 | { }; |
| 59 | //! [actual] |
| 60 | |
| 61 | static_assert(!Printable<void>::value, ""); |
| 62 | } |
| 63 | |
| 64 | int main() { } |
| 65 |
