is_valid.cpp 904 B

12345678910111213141516171819202122232425262728293031
  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/assert.hpp>
  5. #include <boost/hana/not.hpp>
  6. #include <boost/hana/type.hpp>
  7. #include <string>
  8. #include <vector>
  9. namespace hana = boost::hana;
  10. int main() {
  11. // Checking for a member
  12. struct Person { std::string name; };
  13. auto has_name = hana::is_valid([](auto&& p) -> decltype((void)p.name) { });
  14. Person joe{"Joe"};
  15. static_assert(has_name(joe), "");
  16. static_assert(!has_name(1), "");
  17. // Checking for a nested type
  18. auto has_value_type = hana::is_valid([](auto t) -> hana::type<
  19. typename decltype(t)::type::value_type
  20. > { });
  21. static_assert(has_value_type(hana::type_c<std::vector<int>>), "");
  22. static_assert(!has_value_type(hana::type_c<Person>), "");
  23. }