I'm trying to compile the following program:
#include<functional>
#include<iostream>
int main(int argc, char* argv[], char* env[]) {
std::function<int(int, int)> f = [i, &j] { return i + j; };
std::cout << f(5, 5);
}
Why do I get the following error:
a.cc:17:3: error: \u2018function\u2019 is not a member of \u2018std\u2019
Even if I replace it with "auto" the compiler complains that "f" doesn't name a type. I tried with GCC 4.4.3 and 4.6.2.
std::function<int(int, int)> f = [i, &j] { return i + j; };
That is wrong syntax.
What you actually want to write is this:
std::function<int(int, int)> f =[](int i, int j) { return i + j; };
Or if you want to use auto
, then:
auto f =[](int i, int j) { return i + j; };
Use -std=c++0x
option with gcc-4.6.2 to compile this code.
Polymorphic wrappers for function objects are new in C++11. To use these features in a pre-4.7 GCC installation that supports C++0x (the draft version of C++11), you need to compile with the -std=c++0x
switch (see here ).
For GCC v4.7, that is switched to -std=c++11
(see here ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.