userver: utils::Required< T > Class Template Reference
Loading...
Searching...
No Matches
utils::Required< T > Class Template Referencefinal

#include <userver/utils/required.hpp>

Detailed Description

template<typename T>
class utils::Required< T >

A wrapper that holds a value of type T and has no default constructor, forcing explicit initialization.

Unlike std::optional, Required is always engaged after construction. It is useful for struct fields that must be explicitly set.

Example usage

struct MyConfig {
utils::Required<std::string> name;
utils::Required<int> timeout;
};
MyConfig cfg{.name = "my-service", .timeout = 42};
EXPECT_EQ(*cfg.name, "my-service");
EXPECT_EQ(*cfg.timeout, 42);

Definition at line 29 of file required.hpp.

Public Member Functions

template<typename U = T>
requires (!std::same_as<std::decay_t<U>, Required> && std::constructible_from<T, U &&>)
constexpr explicit (!std::convertible_to< U &&, T >) Required(U &&value)
 Construct T from a value (conditionally explicit).
 
template<typename... Args>
requires (sizeof...(Args) >= 2 && std::constructible_from<T, Args && ...>)
constexpr Required (Args &&... args)
 Emplace-construct T from args.
 
 Required (const Required &)=default
 
Requiredoperator= (const Required &)=default
 
 Required (Required &&)=default
 
Requiredoperator= (Required &&)=default
 
constexpr T & operator* () &noexcept USERVER_IMPL_LIFETIME_BOUND
 Access the contained value.
 
constexpr const T & operator* () const &noexcept USERVER_IMPL_LIFETIME_BOUND
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
constexpr T && operator* () &&noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
constexpr T * operator-> () noexcept USERVER_IMPL_LIFETIME_BOUND
 Access members of the contained value.
 
constexpr const T * operator-> () const noexcept USERVER_IMPL_LIFETIME_BOUND
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
constexpr operator T& () &noexcept USERVER_IMPL_LIFETIME_BOUND
 Implicit conversion to T&.
 
constexpr operator const T & () const &noexcept USERVER_IMPL_LIFETIME_BOUND
 Implicit conversion to const T&.
 
template<typename U>
requires (!std::same_as<U, T> && !std::is_reference_v<U> && std::convertible_to<const T&, U>)
constexpr operator U () const
 Implicit conversion to any type that T is implicitly convertible to (by value).
 

Constructor & Destructor Documentation

◆ Required()

template<typename T>
template<typename... Args>
requires (sizeof...(Args) >= 2 && std::constructible_from<T, Args && ...>)
utils::Required< T >::Required ( Args &&... args)
inlineexplicitconstexpr

Emplace-construct T from args.

Definition at line 43 of file required.hpp.

Member Function Documentation

◆ explicit()

template<typename T>
template<typename U = T>
requires (!std::same_as<std::decay_t<U>, Required> && std::constructible_from<T, U &&>)
utils::Required< T >::explicit ( !std::convertible_to< U &&, T > ) &&
inlineconstexpr

Construct T from a value (conditionally explicit).

Definition at line 36 of file required.hpp.

◆ operator const T &()

template<typename T>
utils::Required< T >::operator const T & ( ) const &
inlineconstexprnoexcept

Implicit conversion to const T&.

Definition at line 71 of file required.hpp.

◆ operator T&()

template<typename T>
utils::Required< T >::operator T& ( ) &
inlineconstexprnoexcept

Implicit conversion to T&.

Definition at line 68 of file required.hpp.

◆ operator U()

template<typename T>
template<typename U>
requires (!std::same_as<U, T> && !std::is_reference_v<U> && std::convertible_to<const T&, U>)
utils::Required< T >::operator U ( ) const
inlineconstexpr

Implicit conversion to any type that T is implicitly convertible to (by value).

Definition at line 76 of file required.hpp.

◆ operator*() [1/3]

template<typename T>
T && utils::Required< T >::operator* ( ) &&
inlineconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 59 of file required.hpp.

◆ operator*() [2/3]

template<typename T>
T & utils::Required< T >::operator* ( ) &
inlineconstexprnoexcept

Access the contained value.

Definition at line 53 of file required.hpp.

◆ operator*() [3/3]

template<typename T>
const T & utils::Required< T >::operator* ( ) const &
inlineconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 56 of file required.hpp.

◆ operator->() [1/2]

template<typename T>
const T * utils::Required< T >::operator-> ( ) const
inlineconstexprnoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 65 of file required.hpp.

◆ operator->() [2/2]

template<typename T>
T * utils::Required< T >::operator-> ( )
inlineconstexprnoexcept

Access members of the contained value.

Definition at line 62 of file required.hpp.


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