userver: userver/utils/lazy_prvalue.hpp Source File
Loading...
Searching...
No Matches
lazy_prvalue.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/lazy_prvalue.hpp
4/// @brief @copybrief utils::LazyPrvalue
5
6#include <type_traits>
7#include <utility>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace utils {
12
13/// @ingroup userver_universal
14///
15/// @brief Can be used with various emplace functions to allow in-place
16/// constructing a non-movable value using a callable.
17///
18/// @snippet universal/src/utils/lazy_prvalue_test.cpp LazyPrvalue sample
19template <typename Func>
20class LazyPrvalue final {
21 static_assert(!std::is_reference_v<Func>);
22
23public:
24 constexpr explicit LazyPrvalue(const Func& func) : func_(func) {}
25
26 constexpr explicit LazyPrvalue(Func&& func) : func_(std::move(func)) {}
27
28 LazyPrvalue(LazyPrvalue&&) = delete;
29 LazyPrvalue& operator=(LazyPrvalue&&) = delete;
30
31 constexpr /* implicit */ operator std::invoke_result_t<Func&&>() && { return std::move(func_)(); }
32
33private:
34 Func func_;
35};
36
37} // namespace utils
38
39USERVER_NAMESPACE_END