(27) 泛化版本 future《T》, 这个版本是该组模板类的泛化版本,当函数的返回值是 引用或者 void 时,由该类的特化版本处理。因本类的注释较多,为避免翻页,所以单独给出其源码:
struct _Nil {}; // empty struct, for unused argument types,标志类,构成函数的重载
template <class _Ty> // 类型的多次声明,在定义的前面,就有代码要引用
class shared_future;
// template <class _Ty>
// class _State_manager { _Associated_state<_Ty>* _Assoc_state; bool _Get_only_once; };
template <class _Ty> // class that defines a non-copyable asynchronous return object that holds a value
class future : public _State_manager<_Ty> // 类的实例,该实例定义了一个不可复制的异步返回对象,该对象保存一个
{
using _Mybase = _State_manager<_Ty>; // 简化父类名称
public:
/*
template <class>
bool is_reference_v = false; // determine whether type argument is a reference
template <class _Ty>
bool is_reference_v<_Ty&> = true;
template <class _Ty>
bool is_reference_v<_Ty&&> = true;
//--------------------------------------------------------------------
template <class>
bool is_const_v = false; // determine whether type argument is const qualified
template <class _Ty> // 若类型被 const 修饰,则为 true 。但也有编译器规定的例外情形
bool is_const_v<const _Ty> = true; // 函数类型和引用类型就是被 const 修饰,也是 False
//-----------------------------------------------------------------
// only function types and reference types can't be const qualified
template <class _Ty>
bool is_function_v = !is_const_v<const _Ty> && !is_reference_v<_Ty>;
template <class _Ty> // 这就排除了函数类型与引用类型,又非 void ,即为所谓的对象类型
bool is_object_v = is_const_v<const _Ty> && !is_void_v<_Ty>;
template <class _Ty> // 经测试,函数类型与 void 返回 F,其余类型还有引用都返回 T
bool is_destructible_v = __is_destructible(_Ty);
*/
// 此行判断,要求 _Ty 非数组类型,非函数类型,非引用类型,非 void;且可析构
// 这个判断,也是为了区分本模板的特化类型。函数的返回值是引用或 void 时,由本模板的特化版本处理。
static_assert(!is_array_v<_Ty>&& is_object_v<_Ty>&& is_destructible_v<_Ty>,
"T in future<T> must meet the Cpp17Destructible requirements (N4878 [futures.unique.future]/4).");
future() noexcept {} // 默认的构造函数
future(const future&) = delete; // 本 future 的 copy 构造与赋值运算符函数是禁止的,
future& operator=(const future&) = delete; // 但其父类 _State_manager 的 copy 运算是允许的
// 由 futrue 管理的 _State_manager . _Get_only_once 都为 true
// 本函数算是非典型 copy 的有参构造函数
future(const _Mybase& _State, _Nil) : _Mybase(_State, true) {}
future(future&& _Other) : _Mybase(_STD move(_Other), true) {} // 移动构造函数
future& operator=(future&& _Right) noexcept // 移动赋值运算符
{
_Mybase::operator=(_STD move(_Right));
return *this;
}
~future() noexcept {} // 析构函数,无特殊的含义
/*
template <class _Ty>
class _State_manager
{
_Associated_state<_Ty>* _Assoc_state; bool _Get_only_once;
~_State_manager() { if (_Assoc_state) _Assoc_state->_Release(); }
_Ty& _Get_value() { return _Assoc_state->_Get_value(_Get_only_once); }
};
*/
_Ty get() // block until ready then return the stored result or throw the stored exception
{ // 块直到就绪,然后返回存储的结果或抛出存储的异常
future _Local{ _STD move(*this) }; // 先把自己移动掉,包含的指针将为空, local 将是完好的
// 这么做的目的是为了析构 future ,只读取一次数据
return _STD move(_Local._Get_value());
}
/*
template <class _Ty>
class shared_future : public _State_manager<_Ty>
{
using _Mybase = _State_manager<_Ty>; // 简写父类名称
shared_future(future<_Ty>&& _Other) : _Mybase(_STD forward<_Mybase>(_Other)) {}
};
*/
shared_future<_Ty> share() { return shared_future<_Ty>(_STD move(*this)); }
};
(28) future《T&》 与 future《void》 ,这俩时 future 的特化版本:
// class that defines a non-copyable asynchronous return object that holds a reference
template <class _Ty> //类的实例,该实例定义了一个不可复制的异步返回对象,该对象保存一个引用
class future<_Ty&> : public _State_manager<_Ty*> // 当 future 的模板参数是引用时,采用此特化版本
{
using _Mybase = _State_manager<_Ty*>;
public:
future() noexcept {}
future(const future&) = delete;
future& operator=(const future&) = delete;
future(const _Mybase& _State, _Nil) : _Mybase(_State, true) {}
future(future&& _Other) noexcept : _Mybase(_STD move(_Other), true) {}
future& operator=(future&& _Right) { _Mybase::operator=(_STD move(_Right)); return *this; }
~future() noexcept {}
_Ty& get() // block until ready then return the stored result or throw the stored exception
{ // 块直到就绪,然后返回存储的结果或抛出存储的异常
future _Local{ _STD move(*this) };
return *_Local._Get_value();
}
shared_future<_Ty&> share() { return shared_future<_Ty&>(_STD move(*this)); }
};
template <> // class that defines a non-copyable asynchronous return object that does not hold a value
class future<void> : public _State_manager<int> // 定义不保存值的不可复制异步返回对象的
{
using _Mybase = _State_manager<int>;
public:
future() noexcept {}
future(const future&) = delete;
future& operator=(const future&) = delete;
// struct _Nil {}; // empty struct, for unused argument types
future(const _Mybase& _State, _Nil) : _Mybase(_State, true) {}
future(future&& _Other) noexcept : _Mybase(_STD move(_Other), true) {}
future& operator=(future&& _Right) { _Mybase::operator=(_STD move(_Right)); return *this; }
~future() noexcept {}
void get() { future _Local{ _STD move(*this) }; _Local._Get_value(); }
shared_future<void> share() { return shared_future<void>(_STD move(*this)); }
};
(29) 泛化版本 shared_future《T》, 有着较多的注释,故单独给出其源代码:
template <class _Ty> // class that defines a copyable asynchronous return object that holds a value
class shared_future : public _State_manager<_Ty> // 类的实例,它定义了一个可复制的异步返回对象,该对象保存一个值
{
using _Mybase = _State_manager<_Ty>;
public:
static_assert(!is_array_v<_Ty>&& is_object_v<_Ty>&& is_destructible_v<_Ty>,
"T in shared_future<T> must meet the Cpp17Destructible requirements (N4878 [futures.shared.future]/4).");
shared_future() noexcept {} // 默认构造函数
// 接收了一个 future 的右值引用,本类的有参构造函数
// 全局函数 forward 就是无脑给模板形参 _Mybase 加上俩 & 成为: _Mybase&& ,此处就是右值引用
shared_future(future<_Ty>&& _Other) noexcept : _Mybase(_STD forward<_Mybase>(_Other)) {}
shared_future(const shared_future& _Other) : _Mybase(_Other) {} // copy 构造函数,父类是允许 copy 的
shared_future& operator=(const shared_future& _Right) noexcept // copy 赋值运算符函数
{
_Mybase::operator=(_Right);
return *this;
}
shared_future(shared_future&& _Other) : _Mybase(_STD move(_Other)) {} // 移动构造函数
shared_future& operator=(shared_future&& _Right) noexcept // 移动赋值运算符函数
{
_Mybase::operator=(_STD move(_Right));
return *this;
}
~shared_future() noexcept {} // 也是空的析构函数
/*
template <class _Ty>
class _State_manager
{
_Associated_state<_Ty>* _Assoc_state; bool _Get_only_once;
~_State_manager() { if (_Assoc_state) _Assoc_state->_Release(); }
_Ty& _Get_value() { return _Assoc_state->_Get_value(_Get_only_once); }
};
*/
const _Ty& get() { return this->_Get_value(); }
// 注意:本成员函数的返回值是函数返回值的左值引用。故没有函数返回值的副本
};
(30) 特化版本 shared_future<T&> , 本文的标题写不下了。先只列出这一个特化模板类。注释很少,主要参考其泛化版本:
template <class _Ty> // class that defines a copyable asynchronous return object that holds a reference
class shared_future<_Ty&> : public _State_manager<_Ty*>
{
using _Mybase = _State_manager<_Ty*>;
public:
shared_future() noexcept {}
shared_future(future<_Ty&>&& _Other) noexcept : _Mybase(_STD forward<_Mybase>(_Other)) {}
shared_future(const shared_future& _Other) noexcept : _Mybase(_Other) {}
shared_future& operator=(const shared_future& _Right) noexcept
{
_Mybase::operator=(_Right);
return *this;
}
shared_future(shared_future&& _Other) noexcept : _Mybase(_STD move(_Other)) {}
shared_future& operator=(shared_future&& _Right) noexcept
{
_Mybase::operator=(_STD move(_Right));
return *this;
}
~shared_future() noexcept {}
_Ty& get() const
{
// block until ready then return the stored result or throw the stored exception
return *this->_Get_value();
}
};
(31)
谢谢