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)
25 : func_(func)
26 {}
27
28 constexpr explicit LazyPrvalue(Func&& func)
29 : func_(std::move(func))
30 {}
31
32 LazyPrvalue(LazyPrvalue&&) = delete;
33 LazyPrvalue& operator=(LazyPrvalue&&) = delete;
34
35 constexpr /* implicit */ operator std::invoke_result_t<Func&&>() && { return std::move(func_)(); }
36
37private:
38 Func func_;
39};
40
41} // namespace utils
42
43USERVER_NAMESPACE_END