From 56ee329d79d076742e60c10974c471cc8119ed05 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 3 Jan 2019 22:49:40 +1100 Subject: [PATCH] Add Optional::value_or_compute for lazy computation of default value --- src/optional.hh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/optional.hh b/src/optional.hh index dfa84694..aef1a751 100644 --- a/src/optional.hh +++ b/src/optional.hh @@ -84,6 +84,9 @@ public: template T value_or(U&& fallback) const { return m_valid ? m_value : T{std::forward(fallback)}; } + template + T value_or_compute(U&& compute_func) const { return m_valid ? m_value : compute_func(); } + void reset() { destruct_ifn(); m_valid = false; } private: