Solved

Simple C++0x question about lambda type

Posted on 2011-09-03
2
256 Views
Last Modified: 2012-05-12
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?


0
Comment
Question by:coder1313514512456
2 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 36479220
Fill and sort are just standard algorithms that are part of C++.

http://www.cplusplus.com/reference/algorithm/fill/

http://www.cplusplus.com/reference/algorithm/sort/

In both cases the 3rd param is just a tenplated parameter. When passing a lamda it is just used as a template argument.

Unless C++0X has introduced a feature I've not yet read about, delegates are not a construct in ANSI standard (unmanaged) C++.
0
 

Author Closing Comment

by:coder1313514512456
ID: 36479317
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now