member_function.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/for_each.hpp>
  6. #include <boost/hana/fuse.hpp>
  7. #include <boost/hana/fwd/accessors.hpp>
  8. #include <boost/hana/pair.hpp>
  9. #include <boost/hana/string.hpp>
  10. #include <boost/hana/tuple.hpp>
  11. #include <string>
  12. namespace hana = boost::hana;
  13. //
  14. // Unit test inspired by http://stackoverflow.com/q/32678647/627587
  15. //
  16. struct Foo {
  17. std::string get_name() const { return "louis"; }
  18. };
  19. namespace boost { namespace hana {
  20. template <>
  21. struct accessors_impl<Foo> {
  22. static auto apply() {
  23. return hana::make_tuple(
  24. hana::make_pair(BOOST_HANA_STRING("get_name"), [](auto const& foo) {
  25. return foo.get_name();
  26. })
  27. );
  28. }
  29. };
  30. }}
  31. int main() {
  32. Foo foo;
  33. hana::for_each(foo, hana::fuse([](auto /*key*/, std::string const& name) {
  34. BOOST_HANA_RUNTIME_CHECK(name == "louis");
  35. }));
  36. }