I have a bit confusion about lambda expression: In C++ primer 5 ed. it is said that :
Classes generated from a lambda expression have a deleted default constructor, deleted assignment operators, and a default destructor. Whether the class has a defaulted deleted copy/move constructor depends in the usual ways on the types of the captured data members (§ 13.1.6, p. 508, and § 13.6.2, p.537).
Now If I try this code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <map>
using namespace std;
int main()
{
auto isOdd = [](int x)->bool{ return x % 2;};
vector<int> vi{10, 8, 5, 6, 7, 24};
copy_if(vi.cbegin(), vi.cend(), ostream_iterator<int>(cout, ", "), isOdd); // why this works even on C++11?
auto greater = [](auto x, auto y) { return x > y; };
std::map<std::string, int, decltype(greater)> map; // this doesn't work?
}
-
I don't know how could a lambda with a deleted-default ctor can be defined this way:
auto pow = [](int x){return x * x;}; cout << pow(5) << endl; // 25
-
Why the above works fine even on C++11 although the lambda there has a deleted default-ctor?
-
How can you translate this lambda into a class for example equivalent to it?
-
But why C++11 complains when passed to map<>?
-
So can anyone explain what happens when a lambda doesn't capture anything what the compilr does? What does it generate?
Aucun commentaire:
Enregistrer un commentaire