userver: utils::LazyPrvalue< Func > Class Template Reference
Loading...
Searching...
No Matches
utils::LazyPrvalue< Func > Class Template Referencefinal

#include <userver/utils/lazy_prvalue.hpp>

Detailed Description

template<typename Func>
class utils::LazyPrvalue< Func >

Can be used with various emplace functions to allow in-place constructing a non-movable value using a callable.

// Suppose that 'NonMovable' comes from an external library.
class NonMovable final {
public:
// The only proper way to obtain a 'NonMovable' is through 'Make' factory
// function.
static NonMovable Make() { return NonMovable(42); }
NonMovable(NonMovable&&) = delete;
NonMovable& operator=(NonMovable&&) = delete;
int Get() const { return value_; }
private:
explicit NonMovable(int value) : value_(value) {}
int value_;
};
TEST(LazyPrvalue, Optional) {
std::optional<NonMovable> opt;
// If 'std::optional', in addition to 'emplace' method, had 'emplace_func',
// then we could use it to in-place construct 'NonMovable' there. Alas,
// 'std::optional' has no such method. But we can obtain its functionality
// using 'utils::LazyPrvalue'.
opt.emplace(utils::LazyPrvalue([] { return NonMovable::Make(); }));
EXPECT_EQ(opt->Get(), 42);
}

Definition at line 20 of file lazy_prvalue.hpp.

Public Member Functions

constexpr LazyPrvalue (const Func &func)
 
constexpr LazyPrvalue (Func &&func)
 
 LazyPrvalue (LazyPrvalue &&)=delete
 
LazyPrvalueoperator= (LazyPrvalue &&)=delete
 
constexpr operator std::invoke_result_t< Func && > () &&
 

Constructor & Destructor Documentation

◆ LazyPrvalue() [1/2]

template<typename Func >
constexpr utils::LazyPrvalue< Func >::LazyPrvalue ( const Func & func)
inlineexplicitconstexpr

Definition at line 24 of file lazy_prvalue.hpp.

◆ LazyPrvalue() [2/2]

template<typename Func >
constexpr utils::LazyPrvalue< Func >::LazyPrvalue ( Func && func)
inlineexplicitconstexpr

Definition at line 26 of file lazy_prvalue.hpp.

Member Function Documentation

◆ operator std::invoke_result_t< Func && >()

template<typename Func >
constexpr utils::LazyPrvalue< Func >::operator std::invoke_result_t< Func && > ( ) &&
inlineconstexpr

Definition at line 31 of file lazy_prvalue.hpp.


The documentation for this class was generated from the following file: