Year Version Year-Delta
1983; 1.0; 0
1989; 2.0; +6 years
1998; C++98; +9 years (first ISO standard)
2003; C++03; +5 years (minor update)
2011; C++11; +8 years (major update)
2014; C++14; +3 years (minor update)
The C++14 version was the first that arrived on schedule. The next major version is planned for 2017. The future looks quite promising for C++.
square = (x) -> x * x
C++ won't want to give up the different kinds of capturing, though, so it's hard to see anything much better than what's available now.In imaginary, never-to-be-implemented language daydreams I toy with the idea of using curly braces and implicit positional arguments to have lambdas look like
square = { _1 * _1 }
and factorial = { (_1 < 2) ? 1 : _0(_1 - 1) }
and with explicit captures and arguments, partial_sort = [<_vec, &ge_vec, pivot](item){
if (item < pivot) lt_vec.add(item)
else ge_vec.add(item)
}
I suspect, though, those ideas are just horrible shortcuts to a Perl-like hell :-)For example, consider something like this relatively common block to do some complex operation on the elements of a vector using some (naive) threading (it might be nicer to first create some lambdas, push them into a queue and then spawn threads to work on that queue, but the syntax is essentially the same):
for(std::size_t i(0); i != somevec.size(); ++i) {
workers.push_back(std::thread([&somevec, i]() mutable { calc(somevec, i); }));
}
That is, we want an anonymous lambda which captures some elements of its environment (by both reference and value, also it’s essentially a closure by now) and call some function on these elements. That lambda doesn’t have any return value and doesn’t take any input arguments. Sure one could throw in an extra lambda somewhere or put an arrow between mutable and {, but I have to admit I don’t see the value. class Example {
let ExtraMul = 5
}
let weakly = Example()
let squareAndABit : Int -> Int = { [weak weakly] in $0 * $0 * weakly!.ExtraMul }
squareAndABit(2) // 20
Positional lambda arguments, implicit returns for single expression arguments, capture lists if you want to alter the default capture mode. It's got really rather nice first class function support.Your mockup code sure looks like Lua.
Am I wrong or misunderstanding? I'm not saying completely but I feel like when I have used Templates I can now use this.
For a more concrete example that doesn't rely on the "authority" of an externally developed template library, consider that templates are also useful for static/compile-time polymorphism (see: Curiously Recurring Template Pattern).
The syntax is a bit arcane, but it does what it needs to. They could've stuck a "lambda" keyword in front of it all, but it's not necessary, and making a new keyword always has the potential to break old code.
#define lambda []
?
If you're going to write C++, then write C++. Don't redefine language constructs to fit the patterns of your other favourite language.
#define begin {
#define end }
too. I would not suggest it though.