summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/issues/github_365.cpp
blob: 63d756f4ea1b8a3770a86f808efd5e40f3223a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/accessors.hpp>
#include <boost/hana/adapt_struct.hpp>
#include <boost/hana/assert.hpp>
#include <boost/hana/at.hpp>
#include <boost/hana/define_struct.hpp>
#include <boost/hana/second.hpp>

#include <cstddef>
#include <type_traits>
namespace hana = boost::hana;


//
// This test makes sure that `hana::accessors` does not decay C-style array
// members to pointers.
//

struct Foo {
    float array[3];
};

BOOST_HANA_ADAPT_STRUCT(Foo,
    array
);

template <std::size_t N>
using FloatArray = float[N];

struct Bar {
    BOOST_HANA_DEFINE_STRUCT(Bar,
        (FloatArray<3>, array)
    );
};

int main() {
    {
        Foo foo = {{1.0f, 2.0f, 3.0f}};
        auto accessors = hana::accessors<Foo>();
        auto get_array = hana::second(hana::at_c<0>(accessors));
        static_assert(std::is_same<decltype(get_array(foo)), float(&)[3]>{}, "");
        float (&array)[3] = get_array(foo);
        BOOST_HANA_RUNTIME_CHECK(array[0] == 1.0f);
        BOOST_HANA_RUNTIME_CHECK(array[1] == 2.0f);
        BOOST_HANA_RUNTIME_CHECK(array[2] == 3.0f);
    }

    {
        Bar bar = {{1.0f, 2.0f, 3.0f}};
        auto accessors = hana::accessors<Bar>();
        auto get_array = hana::second(hana::at_c<0>(accessors));
        static_assert(std::is_same<decltype(get_array(bar)), float(&)[3]>{}, "");
        float (&array)[3] = get_array(bar);
        BOOST_HANA_RUNTIME_CHECK(array[0] == 1.0f);
        BOOST_HANA_RUNTIME_CHECK(array[1] == 2.0f);
        BOOST_HANA_RUNTIME_CHECK(array[2] == 3.0f);
    }
}