Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The lambda return type in C++

Tags:

c++

lambda

Lambda with function bodies that contain anything other than a single return statement that do not specify a return type return void.

via 《C++ Primer》 5th Edition, Page 389.

However, if we write the seemingly equivalent program using an if statement, our code won't compile:

//error: can't deduce the return type for the lambda.

transform(vi.begin(), vi.end(), vi.begin(), [](int i) { if(i < 0) return -i; else return i; } );

via 《C++ Primer》 5th Edition, Page 396.

I write a program in Visual Studio:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(void) {
    vector<int> vi{ 1, -2, 3, -4, 5, -6 };
    /* Is the return type void? */
    transform(vi.begin(), vi.end(), vi.begin(), [](int i) {
                                                    if (i < 0) return -i;
                                                    else return i; });

    for (int i : vi)
        cout << i << " ";
    cout << endl;

    system("pause");
    return 0;
}

But it can correctly run.

And then, I add some statements in Visual Studio:

auto f = [](int i) {
                    if (i < 0) return -i;
                    else return i; };

As I move the cursor to the f, it shows me that the f's return type is int.

Why is this?

I am confused.

like image 306
Sefa Avatar asked Oct 22 '16 19:10

Sefa


People also ask

What is the return type of lambda?

The return type for a lambda is specified using a C++ feature named 'trailing return type'. This specification is optional. Without the trailing return type, the return type of the underlying function is effectively 'auto', and it is deduced from the type of the expressions in the body's return statements.

Is there lambda function in C?

Significance of Lambda Function in C/C++ Lambda Function − Lambda are functions is an inline function that doesn't require any implementation outside the scope of the main program. Lambda Functions can also be used as a value by the variable to store.

What does lambda expression return?

Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

What is the return type of lambda expression C++?

The return type of a lambda expression is automatically deduced. You don't have to use the auto keyword unless you specify a trailing-return-type. The trailing-return-type resembles the return-type part of an ordinary function or member function.


1 Answers

C++ Primer 5th Edition covers C++11, and in C++11, the statement you quoted is true. However, C++14 supports deducing return types in more situations, including when a lambda has multiple return statements, as long as they all return the same type. Presumably your version of Visual Studio supports C++14, or at least this feature of it.

like image 131
hobbs Avatar answered Sep 19 '22 11:09

hobbs