c++ - std future 异常 - 已检索,std bug?

标签 c++ c++11 exception promise c++-standard-library

我正在 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/

相关文章:

c++ - 具有可变参数的外部模板无法编译

python - 用 c++ 重写这个 python 函数似乎让它运行得慢了很多。这合理吗?

c++ - 如何确保 win-builder 使用 c++11 构建我的包?

c++ - 如何减少 C++ 中多重集中元素的数量?

java - 加密 key 大小 DES Java

C# 未处理的异常处理程序,试图写入日志文件

C++ 指针数组不可赋值

c++ - 在 C++ 中初始化为自身的对象

c++ - 从模板包生成所有大小为 N 的子包

android - 解析 json 时避免很多 try-catch 博客