STD未来异常-已检索,STD错误

std future exception - already retrieved, std bug?

本文关键字:STD 错误 检索 未来 异常      更新时间:2023-10-16

我正在尝试捕获已经检索的异常,如http://www.cplusplus.com/reference/future/future_errc/

所示。
try {
    prom.get_future();
    prom.get_future();   // throws std::future_error with future_already_retrieved
}
catch (std::future_error& e) {
    if (e.code() == std::make_error_condition(std::future_errc::future_already_retrieved))
        std::cerr << "[future already retrieved]n";
    else
        std::cerr << "[unknown exception]n";
}

但是我总是收到一个无状态的期望。通过查看std的未来实现:

_Ty& _Get_value() const
{   // return the stored result or throw stored exception
    if (!valid())   // will check if already retrieved, and return false
        _Throw_future_error(make_error_code(future_errc::no_state));
        return (_Assoc_state->_Get_value(_Get_only_once)); // only this
            // method can throw the already retrieved exception but its not
            // being hit because of previous valid() check
}

这是Visual Studio 2013中的一个bug还是一个功能?

从我发现更可靠的cppreference:

如果*this没有共享状态或get_future已经被调用,则抛出异常。

什么异常?

抛出:future_error如果*this没有共享状态,或者如果get_future已经在与*this具有相同共享状态的promise上被调用。

  • (14.1) future_already_retrieve如果get_future已经被调用
  • (14.2) no_state if *this is no shared state

所以这是MSVC2013中的一个bug