Skip to content

Commit 730b4e7

Browse files
committed
Test returning reference from the function (bind_back)
Addresses #81055 (comment)
1 parent 66632ba commit 730b4e7

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

libcxx/test/std/utilities/function.objects/func.bind.partial/bind_back.pass.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@ constexpr void test_basic_bindings() {
9292
}
9393

9494
{ // Basic tests with fundamental types
95-
int n = 2;
96-
int m = 1;
97-
auto add = [](int x, int y) { return x + y; };
98-
auto add_n = [](int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; };
95+
int n = 2;
96+
int m = 1;
97+
int sum = 0;
98+
auto add = [](int x, int y) { return x + y; };
99+
auto add_n = [](int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; };
100+
auto add_ref = [&](int x, int y) -> int& { return sum = x + y; };
101+
auto add_rref = [&](int x, int y) -> int&& { return std::move(sum = x + y); };
99102

100103
auto a = std::bind_back(add, m, n);
101104
assert(a() == 3);
@@ -106,6 +109,14 @@ constexpr void test_basic_bindings() {
106109
auto c = std::bind_back(add_n, n, m);
107110
assert(c(1, 1, 1, 1) == 7);
108111

112+
auto d = std::bind_back(add_ref, n, m);
113+
std::same_as<int&> decltype(auto) dresult(d());
114+
assert(dresult == 3);
115+
116+
auto e = std::bind_back(add_rref, n, m);
117+
std::same_as<int&&> decltype(auto) eresult(e());
118+
assert(eresult == 3);
119+
109120
auto f = std::bind_back(add, n);
110121
assert(f(3) == 5);
111122

@@ -114,6 +125,14 @@ constexpr void test_basic_bindings() {
114125

115126
auto h = std::bind_back(add_n, 1, 1, 1);
116127
assert(h(2, 2, 2) == 9);
128+
129+
auto i = std::bind_back(add_ref, n);
130+
std::same_as<int&> decltype(auto) iresult(i(5));
131+
assert(iresult == 7);
132+
133+
auto j = std::bind_back(add_rref, m);
134+
std::same_as<int&&> decltype(auto) jresult(j(4));
135+
assert(jresult == 5);
117136
}
118137
}
119138

0 commit comments

Comments
 (0)