c++returnstdasync

return in functions with std::async


I have a function that should always return A[0][1]. But I use std::async and gcc says:

test_fibonacci_method/1/main.cpp:147:1: warning: control reaches end of non-void function [-Wreturn-type]

Integer Fib_iterative_n_Matrix(const unsigned& n) {
    if (n < 2) return n; // Fib(0):=0 and Fib(1):=1
    std::array<std::array<Integer,2>,2> A = {1,0,0,1}, FIB = {1,1,1,0};

    auto a = std::async(std::launch::async, [&]() { Matrixpower_A_time_B_pow_x(A, FIB, n); return true; });

    if (a.get()) { // blocks
        return A[0][1]; // fib(n)
    }
}

How can I change the last IF with A[0][1]?


Solution

  • OK, I understand now that the if is unnecessary. And a.get() is also to much, because the result of the lambda is not used a.wait() is better.

    Integer Fib_iterative_n_Matrix(const unsigned& n) {
        if (n < 2) return n; // Fib(0):=0 and Fib(1):=1
        std::array<std::array<Integer,2>,2> A = {1,0,0,1}, FIB = {1,1,1,0};
    
        auto a = std::async(std::launch::async, [&]() { Matrixpower_A_time_B_pow_x(A, FIB, n); return true; });
    
        a.wait(); // blocks
        return A[0][1]; // fib(n)
    }