Is my book's discussion of lambda return types wrong?

后端 未结 5 482
误落风尘
误落风尘 2020-12-07 00:59

My book says this:

Lambdas with function bodies that contain anything other than a single return statement that do not specify a return type return vo

5条回答
  •  再見小時候
    2020-12-07 01:18

    If you use popular compilers (gcc, Visual Studio), you usually don't need to specify return type as long as the compiler is able to determine it unambiguously - like in your example.

    The following example shows a lambda, which requires explicit return type information:

    auto lambda = [](bool b) -> float
        { 
            if (b) 
                return 5.0f; 
            else 
                return 6.0; 
        };
    

    I asked Bjarne Stroustrup regarding this matter, his comment:

    I do not know if C++11 allows the deduction of the return type is there are several return statements with identical return type. If not, that's planned for C++14.

提交回复
热议问题