summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/detail/ebo.cpp
blob: 6f174be402407279e922083dfd5b3a1bef019ef0 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Louis Dionne 2013-2016
// 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/detail/ebo.hpp>

#include <boost/hana/assert.hpp>

#include <string>
#include <type_traits>
#include <utility>
namespace hana = boost::hana;
using hana::detail::ebo;


template <int> struct empty { };
template <int> struct idx;
#ifdef BOOST_HANA_WORKAROUND_MSVC_EMPTYBASE
template <typename ...Bases> struct __declspec(empty_bases) inherit : Bases... { };
#else
template <typename ...Bases> struct inherit : Bases... { };
#endif

static_assert(sizeof(inherit<>) == sizeof(inherit<ebo<idx<0>, empty<0>>>), "");
static_assert(sizeof(inherit<>) == sizeof(inherit<ebo<idx<0>, empty<0>>, ebo<idx<1>, empty<1>>>), "");
static_assert(sizeof(inherit<>) == sizeof(inherit<ebo<idx<0>, empty<0>>, ebo<idx<1>, empty<1>>, ebo<idx<2>, empty<2>>>), "");


int main() {
    // Test default-construction
    {
        constexpr ebo<idx<0>, int> e;
        static_assert(hana::detail::ebo_get<idx<0>>(e) == 0, "");
    }

    // Test construction of a non-empty object
    {
        ebo<idx<0>, std::string> e{"foobar"};
        BOOST_HANA_RUNTIME_CHECK(hana::detail::ebo_get<idx<0>>(e) == "foobar");
    }

    {
        ebo<idx<0>, std::string> e{};
        BOOST_HANA_RUNTIME_CHECK(hana::detail::ebo_get<idx<0>>(e) == "");
    }

    // Test construction of a non default-constructible type
    {
        struct nodefault {
            nodefault() = delete;
            explicit nodefault(char const*) { }
        };
        ebo<idx<0>, nodefault> e{"foobar"};
    }

    // Get lvalue, const lvalue and rvalue with a non-empty type
    {
        ebo<idx<0>, std::string> e{"foobar"};
        std::string& s = hana::detail::ebo_get<idx<0>>(e);
        BOOST_HANA_RUNTIME_CHECK(s == "foobar");
        s = "foobaz";
        BOOST_HANA_RUNTIME_CHECK(hana::detail::ebo_get<idx<0>>(e) == "foobaz");
    }

    {
        ebo<idx<0>, std::string> const e{"foobar"};
        std::string const& s = hana::detail::ebo_get<idx<0>>(e);
        BOOST_HANA_RUNTIME_CHECK(s == "foobar");
    }

    {
        ebo<idx<0>, std::string> e{"foobar"};
        std::string&& s = hana::detail::ebo_get<idx<0>>(std::move(e));
        BOOST_HANA_RUNTIME_CHECK(s == "foobar");
    }

    // Get lvalue, const lvalue and rvalue with an empty type
    {
        ebo<idx<0>, empty<0>> e{};
        empty<0>& s = hana::detail::ebo_get<idx<0>>(e);
        (void)s;
    }

    {
        ebo<idx<0>, empty<0>> const e{};
        empty<0> const& s = hana::detail::ebo_get<idx<0>>(e);
        (void)s;
    }

    {
        ebo<idx<0>, empty<0>> e{};
        empty<0>&& s = hana::detail::ebo_get<idx<0>>(std::move(e));
        (void)s;
    }
}