1
2 // Copyright 2015 Peter Dimov.
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 //
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8
9
10 #if defined(_MSC_VER)
11 #pragma warning( disable: 4244 ) // 'initializing': conversion from 'int' to 'char', possible loss of data
12 #endif
13
14 #include <boost/mp11/tuple.hpp>
15 #include <boost/core/lightweight_test.hpp>
16 #include <tuple>
17 #include <memory>
18 #include <utility>
19 #include <array>
20
main()21 int main()
22 {
23 using boost::mp11::tuple_for_each;
24
25 {
26 std::tuple<int, short, char> tp{ 1, 2, 3 };
27
28 {
29 int s = 0;
30
31 tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
32
33 BOOST_TEST_EQ( s, 123 );
34 }
35
36 {
37 int s = 0;
38
39 tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
40
41 BOOST_TEST_EQ( s, 123 );
42 }
43 }
44
45 {
46 std::tuple<int, short, char> const tp{ 1, 2, 3 };
47
48 {
49 int s = 0;
50
51 tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
52
53 BOOST_TEST_EQ( s, 123 );
54 }
55
56 {
57 int s = 0;
58
59 tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
60
61 BOOST_TEST_EQ( s, 123 );
62 }
63 }
64
65 #if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
66 #else
67
68 {
69 std::tuple<std::unique_ptr<int>, std::unique_ptr<int>, std::unique_ptr<int>> tp{ std::unique_ptr<int>(new int(1)), std::unique_ptr<int>(new int(2)), std::unique_ptr<int>(new int(3)) };
70
71 int s = 0;
72
73 tuple_for_each( std::move(tp), [&]( std::unique_ptr<int> p ){ s = s * 10 + *p; } );
74
75 BOOST_TEST_EQ( s, 123 );
76 }
77
78 #endif
79
80 {
81 std::pair<int, short> tp{ 1, 2 };
82
83 {
84 int s = 0;
85
86 tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
87
88 BOOST_TEST_EQ( s, 12 );
89 }
90
91 {
92 int s = 0;
93
94 tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
95
96 BOOST_TEST_EQ( s, 12 );
97 }
98 }
99
100 {
101 std::pair<int, short> const tp{ 1, 2 };
102
103 {
104 int s = 0;
105
106 tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
107
108 BOOST_TEST_EQ( s, 12 );
109 }
110
111 {
112 int s = 0;
113
114 tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
115
116 BOOST_TEST_EQ( s, 12 );
117 }
118 }
119
120 {
121 std::array<int, 3> tp{{ 1, 2, 3 }};
122
123 {
124 int s = 0;
125
126 tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
127
128 BOOST_TEST_EQ( s, 123 );
129 }
130
131 {
132 int s = 0;
133
134 tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
135
136 BOOST_TEST_EQ( s, 123 );
137 }
138 }
139
140 {
141 std::array<int, 3> const tp{{ 1, 2, 3 }};
142
143 {
144 int s = 0;
145
146 tuple_for_each( tp, [&]( int x ){ s = s * 10 + x; } );
147
148 BOOST_TEST_EQ( s, 123 );
149 }
150
151 {
152 int s = 0;
153
154 tuple_for_each( std::move(tp), [&]( int x ){ s = s * 10 + x; } );
155
156 BOOST_TEST_EQ( s, 123 );
157 }
158 }
159
160 {
161 std::tuple<> tp;
162
163 BOOST_TEST_EQ( tuple_for_each( tp, 11 ), 11 );
164 BOOST_TEST_EQ( tuple_for_each( std::move( tp ), 12 ), 12 );
165 }
166
167 {
168 std::array<int, 0> tp;
169
170 BOOST_TEST_EQ( tuple_for_each( tp, 11 ), 11 );
171 BOOST_TEST_EQ( tuple_for_each( std::move( tp ), 12 ), 12 );
172 }
173
174 return boost::report_errors();
175 }
176