Google Cloud C++ Client  0.4.0
C++ Client Library for Google Cloud Platform
Public Member Functions | List of all members
google::cloud::v0::optional< T > Class Template Reference

A poor's man version of std::optional<T>. More...

#include <google/cloud/optional.h>

Public Member Functions

 optional ()
 
 optional (T const &x)
 
 optional (T &&x) noexcept
 
 optional (optional< T > &&rhs) noexcept
 
 optional (optional< T > const &rhs)
 
 ~optional ()
 
optionaloperator= (optional< T > const &rhs)
 
optionaloperator= (optional< T > &&rhs) noexcept
 
template<typename U = T>
std::enable_if< not std::is_same< optional, typename std::decay< U >::type >::value, optional >::type & operator= (U &&rhs)
 
constexpr T const * operator-> () const
 
T * operator-> ()
 
constexpr T const & operator* () const &
 
T & operator* () &
 
T && operator* () &&
 
T & value () &
 
T const & value () const &
 
T && value () &&
 
T const && value () const &&
 
template<typename U >
constexpr T value_or (U &&default_value) const &
 
template<typename U >
value_or (U &&default_value) &&
 
 operator bool () const
 
bool has_value () const
 
void reset ()
 
T & emplace (T &&value)
 
bool operator== (optional const &rhs) const
 
bool operator!= (optional const &rhs) const
 
bool operator< (optional const &rhs) const
 
bool operator> (optional const &rhs) const
 
bool operator>= (optional const &rhs) const
 
bool operator<= (optional const &rhs) const
 

Detailed Description

template<typename T>
class google::cloud::v0::optional< T >

A poor's man version of std::optional<T>.

This project needs to support C++11 and C++14, so std::optional<> is not available. We cannot use Abseil either, see #232 for the reasons. So we implement a very minimal "optional" class that documents the intent and we will remove it when possible.

Template Parameters
Tthe type of the optional value.
Warning
this is not a drop-in replacement for std::optional<>, it has at least the following defects:
  • it raises std::logic_error instead of std::bad_optional_access.
  • emplace() is a very simple version.
  • It does not have the full complement of assignment operators and constructors required by the standard.
  • It lacks comparison operators.
  • No nullopt_t.
  • No std::swap(), std::make_optional(), or std::hash().
Some of the member functions have a (commented-out) constexpr qualifier. The spec requires them to be constexpr functions, but the spec assumes C++14 (or newer) semantics for non-const constexpr member functions, and we often compile with C++11 semantics.

TODO(#687) - replace with absl::optional<> or std::optional<> when possible.

Definition at line 53 of file optional.h.

Constructor & Destructor Documentation

◆ optional() [1/5]

template<typename T >
google::cloud::v0::optional< T >::optional ( )
inline

Definition at line 55 of file optional.h.

◆ optional() [2/5]

template<typename T >
google::cloud::v0::optional< T >::optional ( T const &  x)
inlineexplicit

Definition at line 56 of file optional.h.

◆ optional() [3/5]

template<typename T >
google::cloud::v0::optional< T >::optional ( T &&  x)
inlineexplicitnoexcept

Definition at line 59 of file optional.h.

◆ optional() [4/5]

template<typename T >
google::cloud::v0::optional< T >::optional ( optional< T > &&  rhs)
inlinenoexcept

Definition at line 62 of file optional.h.

◆ optional() [5/5]

template<typename T >
google::cloud::v0::optional< T >::optional ( optional< T > const &  rhs)
inline

Definition at line 67 of file optional.h.

◆ ~optional()

template<typename T >
google::cloud::v0::optional< T >::~optional ( )
inline

Definition at line 72 of file optional.h.

Member Function Documentation

◆ emplace()

template<typename T >
T& google::cloud::v0::optional< T >::emplace ( T &&  value)
inline

Definition at line 194 of file optional.h.

◆ has_value()

template<typename T >
bool google::cloud::v0::optional< T >::has_value ( ) const
inline

Definition at line 186 of file optional.h.

◆ operator bool()

template<typename T >
google::cloud::v0::optional< T >::operator bool ( ) const
inline

Definition at line 185 of file optional.h.

◆ operator!=()

template<typename T >
bool google::cloud::v0::optional< T >::operator!= ( optional< T > const &  rhs) const
inline

Definition at line 211 of file optional.h.

◆ operator*() [1/3]

template<typename T >
constexpr T const& google::cloud::v0::optional< T >::operator* ( ) const &
inline

Definition at line 141 of file optional.h.

◆ operator*() [2/3]

template<typename T >
T& google::cloud::v0::optional< T >::operator* ( ) &
inline

Definition at line 144 of file optional.h.

◆ operator*() [3/3]

template<typename T >
T&& google::cloud::v0::optional< T >::operator* ( ) &&
inline

Definition at line 151 of file optional.h.

◆ operator->() [1/2]

template<typename T >
constexpr T const* google::cloud::v0::optional< T >::operator-> ( ) const
inline

Definition at line 137 of file optional.h.

◆ operator->() [2/2]

template<typename T >
T* google::cloud::v0::optional< T >::operator-> ( )
inline

Definition at line 140 of file optional.h.

◆ operator<()

template<typename T >
bool google::cloud::v0::optional< T >::operator< ( optional< T > const &  rhs) const
inline

Definition at line 215 of file optional.h.

◆ operator<=()

template<typename T >
bool google::cloud::v0::optional< T >::operator<= ( optional< T > const &  rhs) const
inline

Definition at line 237 of file optional.h.

◆ operator=() [1/3]

template<typename T >
optional& google::cloud::v0::optional< T >::operator= ( optional< T > const &  rhs)
inline

Definition at line 74 of file optional.h.

◆ operator=() [2/3]

template<typename T >
optional& google::cloud::v0::optional< T >::operator= ( optional< T > &&  rhs)
inlinenoexcept

Definition at line 95 of file optional.h.

◆ operator=() [3/3]

template<typename T >
template<typename U = T>
std::enable_if< not std::is_same<optional, typename std::decay<U>::type>::value, optional>::type& google::cloud::v0::optional< T >::operator= ( U &&  rhs)
inline

Definition at line 123 of file optional.h.

◆ operator==()

template<typename T >
bool google::cloud::v0::optional< T >::operator== ( optional< T > const &  rhs) const
inline

Definition at line 201 of file optional.h.

◆ operator>()

template<typename T >
bool google::cloud::v0::optional< T >::operator> ( optional< T > const &  rhs) const
inline

Definition at line 229 of file optional.h.

◆ operator>=()

template<typename T >
bool google::cloud::v0::optional< T >::operator>= ( optional< T > const &  rhs) const
inline

Definition at line 233 of file optional.h.

◆ reset()

template<typename T >
void google::cloud::v0::optional< T >::reset ( )
inline

Definition at line 188 of file optional.h.

◆ value() [1/4]

template<typename T >
T& google::cloud::v0::optional< T >::value ( ) &
inline

Definition at line 155 of file optional.h.

◆ value() [2/4]

template<typename T >
T const& google::cloud::v0::optional< T >::value ( ) const &
inline

Definition at line 159 of file optional.h.

◆ value() [3/4]

template<typename T >
T&& google::cloud::v0::optional< T >::value ( ) &&
inline

Definition at line 163 of file optional.h.

◆ value() [4/4]

template<typename T >
T const&& google::cloud::v0::optional< T >::value ( ) const &&
inline

Definition at line 168 of file optional.h.

◆ value_or() [1/2]

template<typename T >
template<typename U >
constexpr T google::cloud::v0::optional< T >::value_or ( U &&  default_value) const &
inline

Definition at line 174 of file optional.h.

◆ value_or() [2/2]

template<typename T >
template<typename U >
T google::cloud::v0::optional< T >::value_or ( U &&  default_value) &&
inline

Definition at line 180 of file optional.h.