summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/tuple/at.non_const.cpp
blob: 3e47c35d97540c13f9e08875a360970cd955d793 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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/assert.hpp>
#include <boost/hana/tuple.hpp>

#include <string>
namespace hana = boost::hana;


struct Empty {};

struct S {
    constexpr S()
        : a{1, Empty{}},
          k(hana::at_c<0>(a)),
          e(hana::at_c<1>(a))
    { }

    hana::tuple<int, Empty> a;
    int k;
    Empty e;
};

constexpr hana::tuple<int, int> getP () { return { 3, 4 }; }

int main() {
    {
        using T = hana::tuple<int>;
        T t(3);
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 3);
        hana::at_c<0>(t) = 2;
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2);
    }
    {
        using T = hana::tuple<std::string, int>;
        T t("high", 5);
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == "high");
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 5);
        hana::at_c<0>(t) = "four";
        hana::at_c<1>(t) = 4;
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == "four");
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 4);
    }
    {
        using T = hana::tuple<double&, std::string, int>;
        double d = 1.5;
        T t(d, "high", 5);
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 1.5);
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == "high");
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 5);
        hana::at_c<0>(t) = 2.5;
        hana::at_c<1>(t) = "four";
        hana::at_c<2>(t) = 4;
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2.5);
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == "four");
        BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 4);
        BOOST_HANA_RUNTIME_CHECK(d == 2.5);
    }
    // get on a rvalue tuple
    {
        static_assert(hana::at_c<0>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 0, "" );
        static_assert(hana::at_c<1>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 1, "" );
        static_assert(hana::at_c<2>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 2, "" );
        static_assert(hana::at_c<3>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 3, "" );
        static_assert(S().k == 1, "");
        static_assert(hana::at_c<1>(getP()) == 4, "");
    }
    {
        // make sure get<> returns the right types
        struct T { };
        struct U { };
        struct V { };

        hana::tuple<T, U, V> xs{};
        (void)static_cast<T>(hana::at_c<0>(xs));
        (void)static_cast<U>(hana::at_c<1>(xs));
        (void)static_cast<V>(hana::at_c<2>(xs));
    }
}