• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 // Copyright 2015, 2017 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_apply;
24 
25     {
26         std::tuple<int, short, char> tp{ 1, 2, 3 };
27 
28         {
29             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
30             BOOST_TEST_EQ( s, 123 );
31         }
32 
33         {
34             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
35             BOOST_TEST_EQ( s, 123 );
36         }
37     }
38 
39     {
40         std::tuple<int, short, char> const tp{ 1, 2, 3 };
41 
42         {
43             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
44             BOOST_TEST_EQ( s, 123 );
45         }
46 
47         {
48             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
49             BOOST_TEST_EQ( s, 123 );
50         }
51     }
52 
53 #if defined( __clang_major__ ) && __clang_major__ == 3 && __clang_minor__ < 8
54 #else
55 
56     {
57         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)) };
58 
59         int s = tuple_apply( [&]( std::unique_ptr<int> px, std::unique_ptr<int> py, std::unique_ptr<int> pz ){ return 100 * *px + 10 * *py + *pz; }, std::move(tp) );
60         BOOST_TEST_EQ( s, 123 );
61     }
62 
63 #endif
64 
65     {
66         std::pair<int, short> tp{ 1, 2 };
67 
68         {
69             int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, tp );
70             BOOST_TEST_EQ( s, 12 );
71         }
72 
73         {
74             int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, std::move(tp) );
75             BOOST_TEST_EQ( s, 12 );
76         }
77     }
78 
79     {
80         std::pair<int, short> const tp{ 1, 2 };
81 
82         {
83             int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, tp );
84             BOOST_TEST_EQ( s, 12 );
85         }
86 
87         {
88             int s = tuple_apply( [&]( int x, int y ){ return 10 * x + y; }, std::move(tp) );
89             BOOST_TEST_EQ( s, 12 );
90         }
91     }
92 
93     {
94         std::array<int, 3> tp{{ 1, 2, 3 }};
95 
96         {
97             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
98             BOOST_TEST_EQ( s, 123 );
99         }
100 
101         {
102             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
103             BOOST_TEST_EQ( s, 123 );
104         }
105     }
106 
107     {
108         std::array<int, 3> const tp{{ 1, 2, 3 }};
109 
110         {
111             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, tp );
112             BOOST_TEST_EQ( s, 123 );
113         }
114 
115         {
116             int s = tuple_apply( [&]( int x, int y, int z ){ return 100 * x + 10 * y + z; }, std::move(tp) );
117             BOOST_TEST_EQ( s, 123 );
118         }
119     }
120 
121     {
122         std::tuple<> tp;
123 
124         BOOST_TEST_EQ( tuple_apply( []{ return 11; }, tp ), 11 );
125         BOOST_TEST_EQ( tuple_apply( []{ return 12; }, std::move( tp ) ), 12 );
126     }
127 
128     {
129         std::array<int, 0> tp;
130 
131         BOOST_TEST_EQ( tuple_apply( []{ return 11; }, tp ), 11 );
132         BOOST_TEST_EQ( tuple_apply( []{ return 12; }, std::move( tp ) ), 12 );
133     }
134 
135     return boost::report_errors();
136 }
137