mardi 30 décembre 2014

Generic lambdas Vs Standard template functions (What to use and When)

Generic Lambdas are a way of defining lambdas that read as follows:



auto identity = [](auto a){return a;}


Compare this with:



template <typename T>
T Identity(T t){return t;}


or with



template <typename T>
struct Identity { T operator()(T a) { return a; } };


This is my understanding




  1. lambdas allow context capture using [&], [=], etc, I am not sure how this will be used / applied in generic lambdas. Is this the main difference ?




  2. generic lambdas can be cast to function pointers whereas, template specialisation can be cast to function pointers.




A simple real world example will be useful to understand what to use and when.


[Appended] Here is the generic lambda proposal : http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3559.pdf


Aucun commentaire:

Enregistrer un commentaire