github_365.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/accessors.hpp>
  5. #include <boost/hana/adapt_struct.hpp>
  6. #include <boost/hana/assert.hpp>
  7. #include <boost/hana/at.hpp>
  8. #include <boost/hana/define_struct.hpp>
  9. #include <boost/hana/second.hpp>
  10. #include <cstddef>
  11. #include <type_traits>
  12. namespace hana = boost::hana;
  13. //
  14. // This test makes sure that `hana::accessors` does not decay C-style array
  15. // members to pointers.
  16. //
  17. struct Foo {
  18. float array[3];
  19. };
  20. BOOST_HANA_ADAPT_STRUCT(Foo,
  21. array
  22. );
  23. template <std::size_t N>
  24. using FloatArray = float[N];
  25. struct Bar {
  26. BOOST_HANA_DEFINE_STRUCT(Bar,
  27. (FloatArray<3>, array)
  28. );
  29. };
  30. int main() {
  31. {
  32. Foo foo = {{1.0f, 2.0f, 3.0f}};
  33. auto accessors = hana::accessors<Foo>();
  34. auto get_array = hana::second(hana::at_c<0>(accessors));
  35. static_assert(std::is_same<decltype(get_array(foo)), float(&)[3]>{}, "");
  36. float (&array)[3] = get_array(foo);
  37. BOOST_HANA_RUNTIME_CHECK(array[0] == 1.0f);
  38. BOOST_HANA_RUNTIME_CHECK(array[1] == 2.0f);
  39. BOOST_HANA_RUNTIME_CHECK(array[2] == 3.0f);
  40. }
  41. {
  42. Bar bar = {{1.0f, 2.0f, 3.0f}};
  43. auto accessors = hana::accessors<Bar>();
  44. auto get_array = hana::second(hana::at_c<0>(accessors));
  45. static_assert(std::is_same<decltype(get_array(bar)), float(&)[3]>{}, "");
  46. float (&array)[3] = get_array(bar);
  47. BOOST_HANA_RUNTIME_CHECK(array[0] == 1.0f);
  48. BOOST_HANA_RUNTIME_CHECK(array[1] == 2.0f);
  49. BOOST_HANA_RUNTIME_CHECK(array[2] == 3.0f);
  50. }
  51. }