1

Following Kerrek's guide in What is std::promise?. I try similar sample codes but it doesn't achieve what I expect.

That is, the caught future_errc error's member function code() doesn't match its what(). Exactly speaking, the call code() returns correctly and what() returns the information of next bit of code(). I doubt that this is something wrong in Visual Studio 2012.

Sample codes:

std::future<int> test_future_error1()
{
    std::promise<int> prom;
    std::future<int> fu = prom.get_future();

    prom.set_value(5566);

    return fu;
}
//---------------------------------------------------------------
std::future<int> test_future_error2()
{
    std::promise<int> prom;
    std::future<int> fu = prom.get_future();
    std::future<int> fu2 = prom.get_future();   // throw std::future_errc::future_already_retrieved

    prom.set_value(5566);

    return fu;
}
//---------------------------------------------------------------
std::future<int> test_future_error3()
{
    std::promise<int> prom;
    std::future<int> fu = prom.get_future();

    prom.set_value(5566);
    prom.set_value(7788);   // throw std::future_errc::promise_already_satisfied

    return fu;
}
//---------------------------------------------------------------
std::future<int> test_future_error4()
{
    std::promise<int> prom;
    std::future<int> fu = prom.get_future();

    return fu;
}   // throw std::future_errc::broken_promise at fu.get()
//---------------------------------------------------------------
std::future<int> test_future_error5()
{
    std::future<int> fu;

    return fu;
}   // throw std::future_errc::no_state at fu.get()
//---------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    try {

        std::future<int> fu = test_future_error3();
        int a = fu.get();

        std::cout << "a= " << a << std::endl;
    } catch(const std::future_error &e) {

        std::cout << "future_error:" << std::endl;
        if( e.code() == std::future_errc::broken_promise )
            std::cout << "code() == std::future_errc::broken_promise" << std::endl;
        else if( e.code() == std::future_errc::future_already_retrieved )
            std::cout << "code() == std::future_errc::future_already_retrieved" << std::endl;
        else if( e.code() == std::future_errc::promise_already_satisfied )
            std::cout << "code() == std::future_errc::promise_already_satisfied" << std::endl;
        else if( e.code() == std::future_errc::no_state )
            std::cout << "code() == std::future_errc::no_state" << std::endl;
        else
            std::cout << "code() == unknown" << std::endl;
        std::cout << "what() == " << e.what() << std::endl;

    } catch(const std::exception &e) {
        std::cout << "excepton: " << e.what() << std::endl;
    }

    std::system("pause");
    return 0;
}

Result:

future_error:
code() == std::future_errc::promise_already_satisfied
what() == no state
Community
  • 1
  • 1
cbel
  • 1,027
  • 8
  • 15
  • It's undesirable and buggy result, but fully compliant with C++11 Standard. C++11 requires only "`what()` of exception classes return some implementation-defined string." – yohjp Apr 27 '13 at 07:50
  • Why does the sample code have five `test_future_errorX` functions when it only uses one of them? – Pete Becker Apr 27 '13 at 11:12

0 Answers0