Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++ syntax question

Posted on 2016-10-10
9
Medium Priority
?
87 Views
Last Modified: 2016-10-10
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.
0
Comment
Question by:Evan Li
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 41837271
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:

0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41837272
-> 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.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 41837279
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 41837283
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.
0
 

Author Comment

by:Evan Li
ID: 41837285
OK, I found the answer, "->int" means that the lambda function will return a integer. Thanks for your attention.
0
 

Author Closing Comment

by:Evan Li
ID: 41837288
You are right, it is the return type
0
 
LVL 40

Expert Comment

by:evilrix
ID: 41837290
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.
0
 
LVL 30

Expert Comment

by:Pawan Kumar
ID: 41837294
Thnx evilrix ! I got in pointers that why got confused. Thanx for the info.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 41837296
No worries, Pawan. Have a good day, sir.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question