SDK  2.1.4 [async]
CloudBackend Software Development Kit - SDK API for C++
cbe::util::Optional< T > Class Template Reference

Class template Optional manages an optional contained value - i.e., a value that is either present or absent. More...

#include <Optional.h>

Classes

class  BadAccess
 Thrown by value() method when accessing an object that does not contain a value. More...
 

Public Member Functions

 Optional ()=default
 Creates an empty Optional, i.e., no value present.
 
 Optional (const T &value)
 Copy value ctor.
 
 Optional (T &&value) noexcept(std::is_nothrow_move_constructible< T >::value)
 Move value ctor.
 
 Optional (const Optional &rh)
 Copy ctor.
 
 Optional (Optional &&rh) noexcept(std::is_nothrow_move_constructible< T >::value)
 Move ctor.
 
Optionaloperator= (const Optional &rh)
 Copy assignment operator.
 
Optionaloperator= (Optional &&rh) noexcept
 Move assignment operator.
 
T & value ()
 Value access non-const method.
 
T & operator* ()
 
T * operator-> ()
 
const T & value () const
 Value access const method.
 
const T & operator* () const
 
const T * operator-> () const
 
bool hasValue () const noexcept
 Checks whether a value is present.
 
 operator bool () const noexcept
 Checks whether a value is present. More...
 
void reset ()
 Invalidates possible contained value. The contained value will be destructed.
 
void swap (Optional &other) noexcept
 Swaps the contents with those of other.
 
template<typename... ArgTs>
void emplace (ArgTs &&... args)
 Constructs the contained value in-place. If *this already contains a value before the call, the contained value is destroyed by calling its destructor.
 

Detailed Description

template<typename T>
class cbe::util::Optional< T >

Class template Optional manages an optional contained value - i.e., a value that is either present or absent.

Template Parameters
TType of the contained value

Member Function Documentation

◆ operator bool()

template<typename T >
cbe::util::Optional< T >::operator bool ( ) const
inlineexplicitnoexcept

Checks whether a value is present.

cbe::util::Optional<int> inquireInt(const char* prompt) {
while (true) {
std::cout << prompt << ": ";
std::string answer;
std::cin >> answer;
try {
return std::stoi(answer); // I.e., return cbe::util::Optional<int>{std::stoi(answer)}
}
catch (...) {
// Invalid input
return {}; // I.e., return cbe::util::Optional<int>{};
}
}
}
~~~
auto n = inquireInt("Please, give an integer value");
if (n) {
std::cout << "Thanks for the number" << *n << "!\n";
} else {
std::cout << "Not a number!\n";
}
~~~
Class template Optional manages an optional contained value - i.e., a value that is either present or...
Definition: Optional.h:20

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