overload.cpp 794 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/functional/overload.hpp>
  6. #include <iostream>
  7. #include <string>
  8. namespace hana = boost::hana;
  9. auto on_string = [](std::string const& s) {
  10. std::cout << "matched std::string: " << s << std::endl;
  11. return s;
  12. };
  13. auto on_int = [](int i) {
  14. std::cout << "matched int: " << i << std::endl;
  15. return i;
  16. };
  17. auto f = hana::overload(on_int, on_string);
  18. int main() {
  19. // prints "matched int: 1"
  20. BOOST_HANA_RUNTIME_CHECK(f(1) == 1);
  21. // prints "matched std::string: abcdef"
  22. BOOST_HANA_RUNTIME_CHECK(f("abcdef") == std::string{"abcdef"});
  23. }