c++ syntax question

Evan Li
Evan Li used Ask the Experts™
on
I do not know how to read following statement:

    auto t = create_task([]() -> int
    {
        return 42;
    });

I can understand Lamda part of the code, but I could not understand "->int" part, what does it mean, how does it affecting the result? Thank you for any help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
evilrixSenior Software Engineer (Avast)

Commented:
It's a function that is being passed a C++14 Lambda function that returns an int, in this case the value 42.

More information on C++ lambda functions:

Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
-> This is the Arrow Operator. It is a dereference operator that is used exclusively with pointers to objects that have members. This operator serves to access the member of an object directly from its address.  For accessing object variables and methods via pointer to object.

If p_emp is a pointer to an object of type Employee, then to assign the value "zara" to the first_name member of object emp, you would write something as follows:

strcpy(p_emp->first_name, "zara");

Below will also explain you the meaning of :: ,->, : in C++?

Ref - https://www.quora.com/What-is-the-meaning-of-in-C++

I hope it helps.
evilrixSenior Software Engineer (Avast)

Commented:
It could be re-written like this to make it a little clearer:

auto lambda = []() -> int { return 42; }; // lambda is just a variable that holds the lambda function
auto t = create_task(lambda); // this is now passing the function object to the create_task function

Open in new window

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Senior Software Engineer (Avast)
Commented:
Pawan,

I'm afraid your assessment of the code is incorrect. In this case, the -> is not being used to dereference anything, it is the syntax used to specify the return value of an auto return value lambda. This syntax was introduce in C++14.
Evan LiSr SW Engineer

Author

Commented:
OK, I found the answer, "->int" means that the lambda function will return a integer. Thanks for your attention.
Evan LiSr SW Engineer

Author

Commented:
You are right, it is the return type
evilrixSenior Software Engineer (Avast)

Commented:
Indeed, but that's only in this case. That syntax is specifically used to allow the use of the decltype operator to be able to specify the return type of a lambda from one or more of the passed arguments. In this case, it's completely pointless, because the return type isn't being deferred from a passed argument.
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Thnx evilrix ! I got in pointers that why got confused. Thanx for the info.
evilrixSenior Software Engineer (Avast)

Commented:
No worries, Pawan. Have a good day, sir.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial