]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/libcxx/utilities/tuple/tuple.tuple/diagnose_reference_binding.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / libcxx / utilities / tuple / tuple.tuple / diagnose_reference_binding.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: c++98, c++03
11
12 // <tuple>
13
14 // Test the diagnostics libc++ generates for invalid reference binding.
15 // Libc++ attempts to diagnose the following cases:
16 //  * Constructing an lvalue reference from an rvalue.
17 //  * Constructing an rvalue reference from an lvalue.
18
19 #include <tuple>
20 #include <string>
21 #include <functional>
22 #include <cassert>
23
24 static_assert(std::is_constructible<int&, std::reference_wrapper<int>>::value, "");
25 static_assert(std::is_constructible<int const&, std::reference_wrapper<int>>::value, "");
26
27
28 int main() {
29     std::allocator<void> alloc;
30     int x = 42;
31     {
32         std::tuple<int&> t(std::ref(x));
33         assert(&std::get<0>(t) == &x);
34         std::tuple<int&> t1(std::allocator_arg, alloc, std::ref(x));
35         assert(&std::get<0>(t1) == &x);
36     }
37     {
38         auto r = std::ref(x);
39         auto const& cr = r;
40         std::tuple<int&> t(r);
41         assert(&std::get<0>(t) == &x);
42         std::tuple<int&> t1(cr);
43         assert(&std::get<0>(t1) == &x);
44         std::tuple<int&> t2(std::allocator_arg, alloc, r);
45         assert(&std::get<0>(t2) == &x);
46         std::tuple<int&> t3(std::allocator_arg, alloc, cr);
47         assert(&std::get<0>(t3) == &x);
48     }
49     {
50         std::tuple<int const&> t(std::ref(x));
51         assert(&std::get<0>(t) == &x);
52         std::tuple<int const&> t2(std::cref(x));
53         assert(&std::get<0>(t2) == &x);
54         std::tuple<int const&> t3(std::allocator_arg, alloc, std::ref(x));
55         assert(&std::get<0>(t3) == &x);
56         std::tuple<int const&> t4(std::allocator_arg, alloc, std::cref(x));
57         assert(&std::get<0>(t4) == &x);
58     }
59     {
60         auto r = std::ref(x);
61         auto cr = std::cref(x);
62         std::tuple<int const&> t(r);
63         assert(&std::get<0>(t) == &x);
64         std::tuple<int const&> t2(cr);
65         assert(&std::get<0>(t2) == &x);
66         std::tuple<int const&> t3(std::allocator_arg, alloc, r);
67         assert(&std::get<0>(t3) == &x);
68         std::tuple<int const&> t4(std::allocator_arg, alloc, cr);
69         assert(&std::get<0>(t4) == &x);
70     }
71 }