Jean-Marc Lasgouttes <lasgout...@lyx.org> writes:

| Le 23/10/12 21:18, Lars Gullik Bjønnes a écrit :
>> | Concerning auto, I am still not sure that I like it.
>>
>> Liking it took me some 5 seconds.
>>
>> Auto suddenly makes it nice to work with the complex types you get in
>> C++.
>>
>> Imagine:
>>
>> auto func = [](){};
>> func();
>>
>> try to figure out what type func really is. Do you care?
>
| I care that I am wirting code with unknown types and suddenly I might
| be doing something awfully inefficient without knowing it. Plus I
| suspect that people who have not read the standard will not know wheat
| are the cases where auto is fine and where are the ones where "you
| obviously can't because XXXXXXX".
>
| JMarc
>
| PS: are you really telling me that [](){} is something valid? Frightening.

:-) heh

That is the new lambda expressions in C++11. Yes, it looks weird at
first, but are not hard to get used to and lambdas are nice.

Shortest possible C++ program that call a function:

----------
int main(){[](){}();}
----------

                  / capture list
                 /  argument list
                /  /    function body
               /  /    /
auto lambda = [](...){ ... };

example:

---------------
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

// Compile on a newer gcc:
// g++ -std=c++11 lambda_and_more.cpp

int main() {
    std::vector<int> v({9,8,7,6,5,4,3,2,1}); 
    std::for_each(v.begin(), v.end(), [](int i){std::cout << i;});
    std::sort(v.begin(), v.end(), [](int lhs, int rhs){ return lhs < rhs; });
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ","));

    int sum = 0;
    std::for_each(v.begin(), v.end(), [&](int i){ sum += i;});
    std::cout << sum << std::endl;
}
----------------

-- 
   Lgb

Reply via email to