typeid.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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/equal.hpp>
  6. #include <boost/hana/remove_if.hpp>
  7. #include <boost/hana/tuple.hpp>
  8. #include <boost/hana/type.hpp>
  9. #include <string>
  10. namespace hana = boost::hana;
  11. struct Cat { std::string name; };
  12. struct Dog { std::string name; };
  13. struct Fish { std::string name; };
  14. bool operator==(Cat const& a, Cat const& b) { return a.name == b.name; }
  15. bool operator!=(Cat const& a, Cat const& b) { return a.name != b.name; }
  16. bool operator==(Dog const& a, Dog const& b) { return a.name == b.name; }
  17. bool operator!=(Dog const& a, Dog const& b) { return a.name != b.name; }
  18. bool operator==(Fish const& a, Fish const& b) { return a.name == b.name; }
  19. bool operator!=(Fish const& a, Fish const& b) { return a.name != b.name; }
  20. int main() {
  21. hana::tuple<Cat, Fish, Dog, Fish> animals{
  22. Cat{"Garfield"}, Fish{"Jaws"}, Dog{"Beethoven"}, Fish{"Nemo"}
  23. };
  24. auto mammals = hana::remove_if(animals, [](auto const& a) {
  25. return hana::typeid_(a) == hana::type<Fish>{};
  26. });
  27. BOOST_HANA_RUNTIME_CHECK(mammals == hana::make_tuple(Cat{"Garfield"}, Dog{"Beethoven"}));
  28. }