如何脱离std :: experimental :: optional? [英] How to disengage std::experimental::optional?
问题描述
借助Boost,我可以使用以下方法创建一个可选的原位:
With Boost I can create an optional in-place with:
boost::optional<boost::asio::io_service::work> work = boost::in_place(boost::ref(io_service));
并通过以下方式将其脱离:
And disengage it with:
work = boost::none;
有了C ++ 14/实验性支持,我可以改为使用以下方法构造一个可选的就地:
With C++14 / experimental support, I can instead construct an optional in-place with:
std::experimental::optional<boost::asio::io_service::work> work;
work.emplace(boost::asio::io_service::work(io_service));
但是我对如何脱离它感到迷茫...
But I'm at a loss for how to disengage it...
推荐答案
work = std::experimental::nullopt;
应脱离work
.
库基础TS 在[optional.nullopt ]:
should disengage work
.
The library fundamental TS specifies in [optional.nullopt]:
struct
nullopt_t
是用作唯一的空结构类型 类型,以指示可选对象的脱离状态.
The struct
nullopt_t
is an empty structure type used as a unique type to indicate a disengaged state for optional objects.
有一个合适的赋值运算符,[optional.object.assign]:
There is an appropriate assignment operator, [optional.object.assign]:
optional<T>& operator=(nullopt_t) noexcept;
效果:如果启用了*this
,则调用val->T::~T()
销毁所包含的值;否则,调用val->T::~T()
销毁所包含的值.否则没有效果.
Effects: If *this
is engaged calls val->T::~T()
to destroy the contained value; otherwise no effect.
为避免每次都构造一个nullopt_t
对象,已经声明了此类型的常量:
To avoid constructing a nullopt_t
object every time, a constant of this type is already declared:
struct nullopt_t{
请参见下文};
constexpr nullopt_t nullopt(
未指定);
struct nullopt_t{
see below};
constexpr nullopt_t nullopt(
unspecified);
这篇关于如何脱离std :: experimental :: optional?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!