Avatar of coder1313514512456
coder1313514512456Flag for United States of America

asked on 

Simple C++0x question about lambda type

This is just a simple question about lambdas in C++0x (well, before it becomes C++11 or C++0xB or something):

While surfing Stroustrup's site, in his C++0x area he gives the following example code which helps describe lambdas.  I get almost all of it except for the third argument in the methods for fill and sort, not on this side but on the method definition side.

Here's his example:  ( Found at http://www2.research.att.com/~bs/C++0xFAQ.html#lambda )

	void f(vector<Record>& v)
		vector<int> indices(v.size());
		int count = 0;
		fill(indices.begin(),indices.end(),[&count](){ return ++count; });

		// sort indices in the order determined by the name field of the records:
		std::sort(indices.begin(), indices.end(), [&](int a, int b) { return v[a].name<v[b].name; });
		// ...

Open in new window

My understanding (and please correct me if I'm wrong) of the arguments passed in to fill() and sort() are:  the first argument is a pointer to the first element in the vector indices, the second argument points to the last element in indices, and the third is the function method to be used for stepping through the vector in the call to fill() and for comparison in the call to sort().

So my question is:  What does the signature look like for these particular methods for fill() and sort()?

Other questions:
What does this signature actually look like when defined, how would you write this?  Assuming the third argument of each is a delegate, where would you put the delegate defintion?    The lambdas appear to take different argument counts (all of which happen to be by reference in this case, but I'm speaking specifically to argument count), so it seems unlikely they would be the same delegate, unless there exists a generic signature that takes all counts and forms of parameters that I am unaware of ... unless () does in fact do this?

C++Visual C++.NET

Avatar of undefined
Last Comment
Avatar of evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of coder1313514512456


Works for me, thanks!  I realize that the methods are standard, but thanks for the links giving the definitions.  I was thinking of a number of different languages when I wrote that, sorry for the confusion on the templated parameter.  And thanks for the quick response.

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo