#pragma once #include #include template class Lazy { public: using value_type = T; explicit Lazy(std::function createValue) : createValue(createValue) {} explicit operator bool() const { return static_cast(_value); } T& value() { init(); return *_value; } const T& value() const { init(); return *_value; } T* operator->() { return &value(); } const T* operator->() const { return &value(); } T& operator*() { return value(); } const T& operator*() const { return value(); } private: void init() const { std::call_once(initialized, [&] { _value = std::make_unique(createValue()); }); } std::function createValue; mutable std::once_flag initialized; mutable std::unique_ptr _value; };