template <typename T, typename TFactory = DefaultValueFactory<T>>
class Lazy final { /*...*/};
template<std::invocable TFactory>
Lazy(TFactory&&) -> Lazy<std::invoke_result_t<TFactory>, std::decay_t<TFactory>>;
Lazy Arrow = [] { return Cursor(SDL_SYSTEM_CURSOR_ARROW); };
Lazy<Cursor> Arrow = [] { return Cursor(SDL_SYSTEM_CURSOR_ARROW); };
#include <utility>
#include <iostream>
class Cursor {
public:
Cursor(int aShape) noexcept : fShape(aShape) {}
int shape() const noexcept { return fShape; }
private:
int fShape;
};
template <typename T, typename TFactory>
class Lazy {
public:
Lazy(TFactory&& x) : factory(std::move(x)) {}
Lazy(const TFactory& x) : factory(x) {}
T operator()() const { return factory(); }
private:
mutable TFactory factory;
};
template <typename T, typename TFactory>
inline auto lazy(TFactory&& factory)
{
return Lazy<T, std::decay_t<TFactory>>(std::forward<TFactory>(factory));
}
int main()
{
auto arrow = lazy<Cursor>([] { return Cursor(42); } );
std::cout << arrow().shape() << '\n';
return 0;
}