Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::Maybe< T > Class Template Reference

可选值容器,可包含一个值或为空。 More...

#include <maybe.h>

Public Member Functions

 Maybe ()
 默认构造一个空 Maybe。
template<class... Args>
 Maybe (Args &&...args)
 从参数包直接构造一个包含值的 Maybe。
 Maybe (const Maybe &other)
 拷贝构造,深拷贝内部值。
 Maybe (Maybe &&other)
 移动构造,转移所有权。
 Maybe (decltype(nothing))
 从 nothing 标记构造空 Maybe。
T & value ()
 获取内部值的引用(非常量)。
const T & value () const
 获取内部值的引用(常量)。
const Maybeoperator= (const T &value)
 从 T 值赋值(拷贝)。
const Maybeoperator= (T &&value)
 从 T 值赋值(移动)。
Maybeoperator= (const Maybe &other)
 拷贝赋值。
Maybeoperator= (Maybe &&other) noexcept
 移动赋值。
const Maybeoperator= (decltype(nothing))
 赋值为空(nothing)。
void reset ()
 清空 Maybe,使其为空。
template<class... Args>
void reset (Args &&...args)
 重置为新的值(从参数包构造)。
 operator bool () const noexcept
 检查 Maybe 是否包含值。
bool has_value () const noexcept
 检查 Maybe 是否包含值。
T & operator* ()
 解引用获取值(非常量),前置条件:has_value() 为 true。
const T & operator* () const
 解引用获取值(常量),前置条件:has_value() 为 true。
T * operator-> ()
 成员访问运算符(非常量),前置条件:has_value() 为 true。
const T * operator-> () const
 成员访问运算符(常量),前置条件:has_value() 为 true。
template<class U>
value_or (U &&default_value) const
 返回当前值,若为空则返回提供的默认值。
void swap (Maybe &other) noexcept
 交换两个 Maybe 的内容。

Friends

std::ostream & operator<< (std::ostream &os, const Maybe &maybe)
 输出 Maybe 到流。若包含值则输出值,否则输出 "(nothing)"。
std::istream & operator>> (std::istream &is, Maybe &maybe)
 从流读取一个值到 Maybe。

Detailed Description

template<class T>
class console::Maybe< T >

可选值容器,可包含一个值或为空。

Template Parameters
T存储的值类型。

基于 std::unique_ptr 实现,支持拷贝、移动、流输入输出。 访问空 Maybe 会抛出 bad_maybe_access 异常。

Constructor & Destructor Documentation

◆ Maybe() [1/5]

template<class T>
console::Maybe< T >::Maybe ( )
inline

默认构造一个空 Maybe。

◆ Maybe() [2/5]

template<class T>
template<class... Args>
console::Maybe< T >::Maybe ( Args &&... args)
inline

从参数包直接构造一个包含值的 Maybe。

Template Parameters
Args构造 T 所需的参数类型。
Parameters
args转发给 T 的构造函数。

◆ Maybe() [3/5]

template<class T>
console::Maybe< T >::Maybe ( const Maybe< T > & other)
inline

拷贝构造,深拷贝内部值。

Parameters
other源 Maybe。
Note
若 other 为空,则新对象也为空。

◆ Maybe() [4/5]

template<class T>
console::Maybe< T >::Maybe ( Maybe< T > && other)
inline

移动构造,转移所有权。

Parameters
other源 Maybe,移动后为空。

◆ Maybe() [5/5]

template<class T>
console::Maybe< T >::Maybe ( decltype(nothing) )
inline

从 nothing 标记构造空 Maybe。

Parameters
nothing标记。

Member Function Documentation

◆ has_value()

template<class T>
bool console::Maybe< T >::has_value ( ) const
inlinenoexcept

检查 Maybe 是否包含值。

Returns
true 若有值,false 若为空。

◆ operator bool()

template<class T>
console::Maybe< T >::operator bool ( ) const
inlineexplicitnoexcept

检查 Maybe 是否包含值。

Returns
true 若有值,false 若为空。

◆ operator*() [1/2]

template<class T>
T & console::Maybe< T >::operator* ( )
inline

解引用获取值(非常量),前置条件:has_value() 为 true。

◆ operator*() [2/2]

template<class T>
const T & console::Maybe< T >::operator* ( ) const
inline

解引用获取值(常量),前置条件:has_value() 为 true。

◆ operator->() [1/2]

template<class T>
T * console::Maybe< T >::operator-> ( )
inline

成员访问运算符(非常量),前置条件:has_value() 为 true。

◆ operator->() [2/2]

template<class T>
const T * console::Maybe< T >::operator-> ( ) const
inline

成员访问运算符(常量),前置条件:has_value() 为 true。

◆ operator=() [1/5]

template<class T>
Maybe & console::Maybe< T >::operator= ( const Maybe< T > & other)
inline

拷贝赋值。

Parameters
other源 Maybe。
Returns
Maybe& *this。

◆ operator=() [2/5]

template<class T>
const Maybe & console::Maybe< T >::operator= ( const T & value)
inline

从 T 值赋值(拷贝)。

Parameters
value要赋值的值。
Returns
const Maybe& *this。

◆ operator=() [3/5]

template<class T>
const Maybe & console::Maybe< T >::operator= ( decltype(nothing) )
inline

赋值为空(nothing)。

Parameters
nothing标记。
Returns
const Maybe& *this。

◆ operator=() [4/5]

template<class T>
Maybe & console::Maybe< T >::operator= ( Maybe< T > && other)
inlinenoexcept

移动赋值。

Parameters
other源 Maybe,移动后为空。
Returns
Maybe& *this。

◆ operator=() [5/5]

template<class T>
const Maybe & console::Maybe< T >::operator= ( T && value)
inline

从 T 值赋值(移动)。

Parameters
value要赋值的右值。
Returns
const Maybe& *this。

◆ reset() [1/2]

template<class T>
void console::Maybe< T >::reset ( )
inline

清空 Maybe,使其为空。

◆ reset() [2/2]

template<class T>
template<class... Args>
void console::Maybe< T >::reset ( Args &&... args)
inline

重置为新的值(从参数包构造)。

Template Parameters
Args参数类型。
Parameters
args转发给 T 的构造函数。

◆ swap()

template<class T>
void console::Maybe< T >::swap ( Maybe< T > & other)
inlinenoexcept

交换两个 Maybe 的内容。

Parameters
other要交换的 Maybe。

◆ value() [1/2]

template<class T>
T & console::Maybe< T >::value ( )
inline

获取内部值的引用(非常量)。

Returns
T& 值的引用。
Exceptions
bad_maybe_accessMaybe 为空。

◆ value() [2/2]

template<class T>
const T & console::Maybe< T >::value ( ) const
inline

获取内部值的引用(常量)。

Returns
const T& 值的常量引用。
Exceptions
bad_maybe_accessMaybe 为空。

◆ value_or()

template<class T>
template<class U>
T console::Maybe< T >::value_or ( U && default_value) const
inline

返回当前值,若为空则返回提供的默认值。

Template Parameters
U默认值类型。
Parameters
default_value默认值(可转发)。
Returns
T 当前值或转换后的默认值。

◆ operator<<

template<class T>
std::ostream & operator<< ( std::ostream & os,
const Maybe< T > & maybe )
friend

输出 Maybe 到流。若包含值则输出值,否则输出 "(nothing)"。

Parameters
os输出流。
maybeMaybe 对象。
Returns
std::ostream& os。

◆ operator>>

template<class T>
std::istream & operator>> ( std::istream & is,
Maybe< T > & maybe )
friend

从流读取一个值到 Maybe。

Parameters
is输入流。
maybe目标 Maybe。
Returns
std::istream& is。

尝试从流读取一个 T 类型的值,若成功则存入 Maybe,否则将 Maybe 置空并清除错误标志。


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