Archive for June 2011
C++11 new features
Lambda expressions
Definition: Anonymous functions:
[capture](parameters)->return_type {body}
The parameter capture is defined as following:
[] //no variables defined. Using one will result in error
[x, &y] //x is captured by value, y is captured by reference
[&] //any external variable is implicitly captured by reference if used
[=] //any external variable is implicitly captured by value if used
[&, x] //x is explicitly captured by value. Other variables will be captured by reference
[=, &z] //z is explicitly captured by reference. Other variables will be captured by value
Example:
char s[]="Hello World!"; int Uppercase = 0; //modified by the lambda for_each(s, s+sizeof(s), [&Uppercase] (char c) { if (isupper(c)) Uppercase++; }); cout<< Uppercase<<" uppercase letters in: "<< s<<endl;
Automatic type deduction: auto
auto x=0; //x has type int because 0 is int auto c='a'; //char auto d=0.5; //double auto national_debt=14400000000000LL;//long long
Object or expression type: decltype
const vector<int> vi; typedef decltype (vi.begin()) CIT; CIT another_const_iterator;
Initialization
vector vs<string>={ "first", "second", "third"}; map singers = { {"Lady Gaga", "+1 (212) 555-7890"}, {"Beyonce Knowles", "+1 (212) 555-0987"}}; class C { int a=7; //Initialization in the header public: C(); };