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