【发布时间】:2015-09-18 09:21:07
【问题描述】:
我正在尝试捕获在 http://www.cplusplus.com/reference/future/future_errc/ 中看到的 Already-Retrieved 异常
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 中的错误还是功能?
【问题讨论】:
-
你应该看
std::promise或std::packaged_task的实现(具体来说,std::promise::get_future或std::packaged_task::get_future的实现),而不是std::future之一。 -
是的,这在我看来像是一个 Visual Studio 错误。
标签: c++ c++11 exception-handling promise c++-standard-library