Skip to content

Commit 4ecb39d

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 4ecb39d

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-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 funt>
49+
auto lazy(funt fun) -> lazyt<decltype(fun())>
50+
{
51+
return lazyt<decltype(fun())>::from_fun(std::move(fun));
52+
}
53+
4654
#endif // CPROVER_UTIL_LAZY_H

unit/util/lazy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ 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<std::size_t> lazy_length =
20+
lazy([&] { return length_with_counter("foo"); });
2121

2222
REQUIRE(call_counter == 0);
2323
auto result = lazy_length.force();

0 commit comments

Comments
 (0)