-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfwd_capture.cc
42 lines (34 loc) · 938 Bytes
/
fwd_capture.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//-----------------------------------------------------------------------------
//
// Source code for MIPT masters course on C++
// Slides: https://sourceforge.net/projects/cpp-lects-rus
// Licensed after GNU GPL v3
//
//-----------------------------------------------------------------------------
//
// Problem of capture list forward: solved but little hacky
//
//----------------------------------------------------------------------------
#include "gtest/gtest.h"
#include <utility>
namespace {
template <typename T> auto fwd_capture(T &&x) {
struct CapT {
T value;
};
return CapT{std::forward<T>(x)};
}
} // namespace
TEST(lamdas, fwd_capture) {
auto foo = []<typename T>(T &&a) {
return [a = fwd_capture(a)]() mutable { return ++a.value; };
};
int x = 1;
auto f1 = foo(1);
EXPECT_EQ(f1(), 2);
auto f2 = foo(x);
EXPECT_EQ(f2(), 2);
EXPECT_EQ(f2(), 3);
EXPECT_EQ(f2(), 4);
EXPECT_EQ(x, 4);
}