Skip to content

Commit 63147b4

Browse files
Add lazy function with auto inferred type
This allow to write `lazy(f)` and get the type of the object automatically inferred from the type of `f`.
1 parent 1b4de42 commit 63147b4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/util/lazy.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ class lazyt
4343
}
4444
};
4545

46+
/// Delay the computation of \p fun to the next time the \c force method
47+
/// is called.
48+
template<typename valuet>
49+
auto lazy(std::function<valuet()> fun) -> decltype(fun())
50+
{
51+
return lazyt<valuet>::from_fun(std::move(fun));
52+
}
53+
4654
#endif // CPROVER_UTIL_LAZY_H

unit/util/lazy.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ SCENARIO("lazy test", "[core][util][lazy]")
1616
++call_counter;
1717
return s.length();
1818
};
19-
auto lazy_length =
20-
lazyt<int>::from_fun([&]() { return length_with_counter("foo"); });
19+
lazyt<int> lazy_length = lazy([&] { return length_with_counter("foo"); });
2120

2221
REQUIRE(call_counter == 0);
2322
auto result = lazy_length.force();

0 commit comments

Comments
 (0)