Topic: clarification about lambda proposal (n2320)


Author: gpderetta <gpderetta@gmail.com>
Date: Thu, 6 Sep 2007 20:23:11 CST
Raw View
Hi all,

I've recently read the last lambda proposal. I think it looks very
good (especially compared to the previous iterations that lacked
polymorphic lambdas), and would like a clarification.

By reading the paper it seems that there is no explicit provision for
variadic lambdas. Probably this is already implied by the translation
semantics described in section 5 of the paper, but I wanted to make
sure that lambdas will really support this usage.

Variadic lambda would be very useful as forwarding stubs: the current
language doesn't allow  overloaded function or function template to be
passed to generic algorithm, without loosing their polymorphic
behavior.

To solve the problem you need to write explicit out of line forwarding
functors. Variadic templates and rvalue references will make writing
these forwarding functions easier, but without variadic lambdas, you
still need to give a name to these stubs and write them out of line.
Also you have to explicitly deal with decltype to figure out the
result type.

With variadic lambdas it will really be easier:

--- begin example ---

template<typename F>
void bar(F f) {
   int i = 10;
   std::tuple<int, double> j (0, 0.0);
   f(i);
   f(j);
   f(j, i);
}

template<typename X>
void foo(X x) {
   std::cout << x;
}

template<typename X, typename Y>
void foo(std::tuple<X, Y> p) {
   foo(std::get<0>(p), std::get<1>(p));
}

int main() {
    bar(<>(args...){ foo(args) });
}

---- end example ----

The example doesn't do anything useful, also I do not really think
I've got the syntax for variadic templates right, but I hope you get
the idea.

If the above is already supposed to work with the current proposal, it
is great, but I still have a small
suggestion. Is it be feasible to have the following syntax (or
something similar) translate to the above call to 'bar'?

   bar( <>foo );

That is, a lambda symbol followed by an (optionally namespace
qualified) function name stands
for an unnamed lambda functor that forwards to a call to that
function. It is just a shorthand, and doesn't add much to the full
lambda syntax, but would be a simple notation for a simple thing.

Making it possible to easily represent an overload set and/or function
template, I think it would really close an ugly shortcoming of the
language.

Hoping-that-the-above-make-sensely yours,

Giovanni P. Deretta

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]