1 // Copyright (C) 2019-2021 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
24 #include <testsuite_hooks.h>
26 // P1651R0 bind_front should not unwrap reference_wrapper
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"
34 void functionAcceptingStringView(std::string_view
) { }
40 auto fs
= std::bind_front(&functionAcceptingStringView
, std::string_view(s
));
46 PartialApply(F f
) : f(f
) {}
49 template <typename
... A
> decltype(auto) operator()(A
const&... a
) const {
50 if constexpr (std::is_invocable
<F
const&, A
const&...>::value
) {
53 return bind_front(*this, a
...);
62 std::unique_ptr
<Thingy
> thingy
;
63 auto func
= [](const std::unique_ptr
<Thingy
>&, int) {};
64 PartialApply
{func
}(std::ref(thingy
))(10);
71 auto func
= [](const std::string
& s
, int) -> const std::string
& { return s
; };
73 // sref refers to copy of str stored in bind_front result:
74 const std::string
& sref
= PartialApply
{func
}(std::ref(str
))(10);
76 // pre-P1651R0 this is a use of a dangling reference:
77 const char& c
= sref
[0];
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
);