File tree 2 files changed +28
-1
lines changed 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -43,4 +43,12 @@ class lazyt
43
43
}
44
44
};
45
45
46
+ // / Delay the computation of \p fun to the next time the \c force method
47
+ // / is called.
48
+ template <typename funt>
49
+ auto lazy (funt fun) -> lazyt<decltype(fun())>
50
+ {
51
+ return lazyt<decltype (fun ())>::from_fun (std::move (fun));
52
+ }
53
+
46
54
#endif // CPROVER_UTIL_LAZY_H
Original file line number Diff line number Diff line change 9
9
#include < testing-utils/use_catch.h>
10
10
#include < util/lazy.h>
11
11
12
- SCENARIO (" lazy test" , " [core][util][lazy]" )
12
+ SCENARIO (" lazyt::from_fun test" , " [core][util][lazy]" )
13
13
{
14
14
std::size_t call_counter = 0 ;
15
15
auto length_with_counter = [&call_counter](const std::string &s) {
@@ -27,3 +27,22 @@ SCENARIO("lazy test", "[core][util][lazy]")
27
27
REQUIRE (call_counter == 1 );
28
28
REQUIRE (result == 3 );
29
29
}
30
+
31
+ SCENARIO (" lazy test" , " [core][util][lazy]" )
32
+ {
33
+ std::size_t call_counter = 0 ;
34
+ auto length_with_counter = [&call_counter](const std::string &s) {
35
+ ++call_counter;
36
+ return s.length ();
37
+ };
38
+ lazyt<std::size_t > lazy_foobar_length =
39
+ lazy ([&] { return length_with_counter (" foobar" ); });
40
+
41
+ REQUIRE (call_counter == 0 );
42
+ auto result = lazy_length.force ();
43
+ REQUIRE (call_counter == 1 );
44
+ REQUIRE (result == 6 );
45
+ result = lazy_length.force ();
46
+ REQUIRE (call_counter == 1 );
47
+ REQUIRE (result == 6 );
48
+ }
You can’t perform that action at this time.
0 commit comments