| 1 |
// Copyright (C) 2019-2025 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-do run { target c++20 } } |
| 19 |
// { dg-add-options no_pch } |
| 20 |
|
| 21 |
#include <functional> |
| 22 |
|
| 23 |
// P1651R0 bind_front should not unwrap reference_wrapper |
| 24 |
|
| 25 |
#ifndef __cpp_lib_bind_front |
| 26 |
# error "Feature test macro for bind_front is missing in <functional>" |
| 27 |
#elif __cpp_lib_bind_front < 201907L |
| 28 |
# error "Feature test macro for bind_front has wrong value in <functional>" |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <memory> |
| 32 |
#include <string> |
| 33 |
#include <testsuite_hooks.h> |
| 34 |
|
| 35 |
void functionAcceptingStringView(std::string_view) { } |
| 36 |
|
| 37 |
void |
| 38 |
test01() |
| 39 |
{ |
| 40 |
std::string s; |
| 41 |
auto fs = std::bind_front(&functionAcceptingStringView, std::string_view(s)); |
| 42 |
fs(); |
| 43 |
} |
| 44 |
|
| 45 |
template <typename F> |
| 46 |
struct PartialApply { |
| 47 |
PartialApply(F f) : f(f) {} |
| 48 |
F f; |
| 49 |
|
| 50 |
template <typename... A> decltype(auto) operator()(A const&... a) const { |
| 51 |
if constexpr (std::is_invocable<F const&, A const&...>::value) { |
| 52 |
return f(a...); |
| 53 |
} else { |
| 54 |
return bind_front(*this, a...); |
| 55 |
} |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
void |
| 60 |
test02() |
| 61 |
{ |
| 62 |
struct Thingy { }; |
| 63 |
std::unique_ptr<Thingy> thingy; |
| 64 |
auto func = [](const std::unique_ptr<Thingy>&, int) {}; |
| 65 |
PartialApply{func}(std::ref(thingy))(10); |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
test03() |
| 70 |
{ |
| 71 |
std::string str; |
| 72 |
auto func = [](const std::string& s, int) -> const std::string& { return s; }; |
| 73 |
|
| 74 |
// sref refers to copy of str stored in bind_front result: |
| 75 |
const std::string& sref = PartialApply{func}(std::ref(str))(10); |
| 76 |
|
| 77 |
// pre-P1651R0 this is a use of a dangling reference: |
| 78 |
const char& c = sref[0]; |
| 79 |
|
| 80 |
// post-P1651R0 the bind_front result stores a reference_wrapper by value, |
| 81 |
// and so sref is bound to str instead of dangling: |
| 82 |
VERIFY( &c == str.data() ); |
| 83 |
VERIFY( &sref == &str ); |
| 84 |
} |
| 85 |
|
| 86 |
int |
| 87 |
main() |
| 88 |
{ |
| 89 |
test01(); |
| 90 |
test02(); |
| 91 |
test03(); |
| 92 |
} |