我正在 try catch 已检索的异常,如 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 future 的实现:
_Ty& _Get_value() const
{ // return the stored result or throw stored exception
if (!valid()) // <b>will check if already retrieved, and return false</b>
_Throw_future_error(make_error_code(future_errc::no_state));
return (_Assoc_state->_Get_value(_Get_only_once)); // <b>only this
// method can throw the already retrieved exception but its not
// being hit because of previous valid() check</b>
}
这是 Visual Studio 2013 中的错误还是功能?
最佳答案
来自cppreference我觉得更可靠:
Exception is thrown if *this has no shared state or get_future has already been called.
什么异常(exception)?
Throws: future_error if
*this
has no shared state or if get_future has already been called on a promise with the same shared state as *this.
- (14.1) future_already_retrieved if get_future has already been called on a promise with the same shared state as *this .
- (14.2) no_state if *this has no shared state.
所以这是 MSVC2013 中的一个错误。
关于c++ - std future 异常 - 已检索,std bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32648386/