gpt4 book ai didi

c++ - 如何脱离 std::experimental::optional?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:59:30 24 4
gpt4 key购买 nike

使用 Boost,我可以创建一个可选的就地:

boost::optional<boost::asio::io_service::work> work = boost::in_place(boost::ref(io_service));

并通过以下方式解除它:

work = boost::none;

有了 C++14/实验性支持,我可以改为构建一个可选的就地:

std::experimental::optional<boost::asio::io_service::work> work;
work.emplace(boost::asio::io_service::work(io_service));

但我不知道如何解除它...

最佳答案

work = std::experimental::nullopt;

应该停止工作
library fundamental TS在 [optional.nullopt] 中指定:

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]:

optional<T>& operator=(nullopt_t) noexcept;

Effects: If *this is engaged calls val->T::~T() to destroy the contained value; otherwise no effect.

为了避免每次都构造一个nullopt_t对象,已经声明了一个这种类型的常量:

struct nullopt_t{see below};
constexpr nullopt_t nullopt(unspecified);

关于c++ - 如何脱离 std::experimental::optional?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26897066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com