concepts.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright Louis Dionne 2013-2017
  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. #include <boost/hana/core/default.hpp>
  5. #include <boost/hana/core/tag_of.hpp>
  6. #include <boost/hana/integral_constant.hpp>
  7. #include <string>
  8. #include <type_traits>
  9. namespace hana = boost::hana;
  10. namespace with_special_base_class {
  11. //! [special_base_class]
  12. struct special_base_class { };
  13. template <typename T>
  14. struct print_impl : special_base_class {
  15. template <typename ...Args>
  16. static constexpr auto apply(Args&& ...) = delete;
  17. };
  18. template <typename T>
  19. struct Printable
  20. : hana::integral_constant<bool,
  21. !std::is_base_of<special_base_class, print_impl<hana::tag_of_t<T>>>::value
  22. >
  23. { };
  24. //! [special_base_class]
  25. //! [special_base_class_customize]
  26. struct Person { std::string name; };
  27. template <>
  28. struct print_impl<Person> /* don't inherit from special_base_class */ {
  29. // ... implementation ...
  30. };
  31. static_assert(Printable<Person>::value, "");
  32. static_assert(!Printable<void>::value, "");
  33. //! [special_base_class_customize]
  34. }
  35. namespace actual {
  36. //! [actual]
  37. template <typename T>
  38. struct print_impl : hana::default_ {
  39. template <typename ...Args>
  40. static constexpr auto apply(Args&& ...) = delete;
  41. };
  42. template <typename T>
  43. struct Printable
  44. : hana::integral_constant<bool,
  45. !hana::is_default<print_impl<hana::tag_of_t<T>>>::value
  46. >
  47. { };
  48. //! [actual]
  49. static_assert(!Printable<void>::value, "");
  50. }
  51. int main() { }