]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/function_objects/bind_front/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / function_objects / bind_front / 2.cc
1 // Copyright (C) 2019-2021 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20
21 #include <functional>
22 #include <memory>
23 #include <string>
24 #include <testsuite_hooks.h>
25
26 // P1651R0 bind_front should not unwrap reference_wrapper
27
28 #ifndef __cpp_lib_bind_front
29 # error "Feature test macro for bind_front is missing"
30 #elif __cpp_lib_bind_front < 201907L
31 # error "Feature test macro for bind_front has wrong value"
32 #endif
33
34 void functionAcceptingStringView(std::string_view) { }
35
36 void
37 test01()
38 {
39 std::string s;
40 auto fs = std::bind_front(&functionAcceptingStringView, std::string_view(s));
41 fs();
42 }
43
44 template <typename F>
45 struct PartialApply {
46 PartialApply(F f) : f(f) {}
47 F f;
48
49 template <typename... A> decltype(auto) operator()(A const&... a) const {
50 if constexpr (std::is_invocable<F const&, A const&...>::value) {
51 return f(a...);
52 } else {
53 return bind_front(*this, a...);
54 }
55 }
56 };
57
58 void
59 test02()
60 {
61 struct Thingy { };
62 std::unique_ptr<Thingy> thingy;
63 auto func = [](const std::unique_ptr<Thingy>&, int) {};
64 PartialApply{func}(std::ref(thingy))(10);
65 }
66
67 void
68 test03()
69 {
70 std::string str;
71 auto func = [](const std::string& s, int) -> const std::string& { return s; };
72
73 // sref refers to copy of str stored in bind_front result:
74 const std::string& sref = PartialApply{func}(std::ref(str))(10);
75
76 // pre-P1651R0 this is a use of a dangling reference:
77 const char& c = sref[0];
78
79 // post-P1651R0 the bind_front result stores a reference_wrapper by value,
80 // and so sref is bound to str instead of dangling:
81 VERIFY( &c == str.data() );
82 VERIFY( &sref == &str );
83 }
84
85 int
86 main()
87 {
88 test01();
89 test02();
90 test03();
91 }