summaryrefslogtreecommitdiff
path: root/test/libcxx/utilities/tuple/tuple.tuple/tuple.cnstr/enable_reduced_arity_initialization_extension.pass.cpp
blob: 99b6eb78f26887d233ac9a8b673f1c71181c4a45 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <tuple>

// template <class... Types> class tuple;

// template <class... UTypes>
//   explicit tuple(UTypes&&... u);

// UNSUPPORTED: c++98, c++03

// MODULES_DEFINES: _LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION
#define _LIBCPP_ENABLE_TUPLE_IMPLICIT_REDUCED_ARITY_EXTENSION
#include <tuple>
#include <cassert>
#include <type_traits>
#include <string>
#include <system_error>

#include "test_macros.h"
#include "test_convertible.hpp"
#include "MoveOnly.h"


struct NoDefault { NoDefault() = delete; };


// Make sure the _Up... constructor SFINAEs out when the types that
// are not explicitly initialized are not all default constructible.
// Otherwise, std::is_constructible would return true but instantiating
// the constructor would fail.
void test_default_constructible_extension_sfinae()
{
    typedef MoveOnly MO;
    typedef NoDefault ND;
    {
        typedef std::tuple<MO, ND> Tuple;
        static_assert(!std::is_constructible<Tuple, MO>::value, "");
        static_assert(std::is_constructible<Tuple, MO, ND>::value, "");
        static_assert(test_convertible<Tuple, MO, ND>(), "");
    }
    {
        typedef std::tuple<MO, MO, ND> Tuple;
        static_assert(!std::is_constructible<Tuple, MO, MO>::value, "");
        static_assert(std::is_constructible<Tuple, MO, MO, ND>::value, "");
        static_assert(test_convertible<Tuple, MO, MO, ND>(), "");
    }
    {
        // Same idea as above but with a nested tuple type.
        typedef std::tuple<MO, ND> Tuple;
        typedef std::tuple<MO, Tuple, MO, MO> NestedTuple;

        static_assert(!std::is_constructible<
            NestedTuple, MO, MO, MO, MO>::value, "");
        static_assert(std::is_constructible<
            NestedTuple, MO, Tuple, MO, MO>::value, "");
    }
    {
        typedef std::tuple<MO, int> Tuple;
        typedef std::tuple<MO, Tuple, MO, MO> NestedTuple;

        static_assert(std::is_constructible<
            NestedTuple, MO, MO, MO, MO>::value, "");
        static_assert(test_convertible<
            NestedTuple, MO, MO, MO, MO>(), "");

        static_assert(std::is_constructible<
            NestedTuple, MO, Tuple, MO, MO>::value, "");
        static_assert(test_convertible<
            NestedTuple, MO, Tuple, MO, MO>(), "");
    }
}

std::tuple<std::string, int, std::error_code> doc_example() {
      return {"hello world", 42};
}

// Test that the example given in UsingLibcxx.rst actually works.
void test_example_from_docs() {
  auto tup = doc_example();
  assert(std::get<0>(tup) == "hello world");
  assert(std::get<1>(tup) == 42);
  assert(std::get<2>(tup) == std::error_code{});
}

int main()
{

    {
        using E = MoveOnly;
        using Tup = std::tuple<E, E, E>;
        static_assert(test_convertible<Tup, E, E, E>(), "");

        Tup t = {E(0), E(1)};
        static_assert(test_convertible<Tup, E, E>(), "");
        assert(std::get<0>(t) == 0);
        assert(std::get<1>(t) == 1);
        assert(std::get<2>(t) == MoveOnly());

        Tup t2 = {E(0)};
        static_assert(test_convertible<Tup, E>(), "");
        assert(std::get<0>(t) == 0);
        assert(std::get<1>(t) == E());
        assert(std::get<2>(t) == E());
    }
    // Check that SFINAE is properly applied with the default reduced arity
    // constructor extensions.
    test_default_constructible_extension_sfinae();
    test_example_from_docs();
}