我想保存lambda表达式变量(就像在第一个代码块中)。问题是,然后我使用一个类(如第二个代码块),编译器返回一些错误。我不知道怎么修理它。
我希望有人能帮我解释一下,为什么不是这样的。谢谢。
第一法典:
// variable for function pointer
void (*func)(int);
// default output function
void my_default(int x) {
cout << "x =" << "\t" << x << endl << endl;
}
int main() {
cout << "Test Programm\n\n";
// 1. Test - default output function
cout << "my_default\n";
func = &my_default;
func(5);
// 2. Test - special output function 2
cout << "my_func2\n";
func = [](int x) { cout << "x =" << " " << x << endl << endl; };
func(5);
return 0;
}第二部法典:
class test {
private:
// variable for function pointer
void (*func)(int);
// default output function
void my_default(int x) {
cout << "x =" << "\t" << x << endl << endl;
}
public:
void dummy(void) {
// 1. Test - default output function
cout << "my_default\n";
func = &my_default;
func(5);
// 2. Test - special output function 2
cout << "my_func2\n";
func = [](int x)->int{ cout << "x =" << " " << x << endl << endl; };
func(5);
}
};
// entry
int main() {
cout << "Test Programm\n\n";
test a;
a.dummy();
return 0;
}编译器:
pi@raspberrypi ~/dev/property $ gcc -std=c++0x -o test2 test2.cpp -lstdc++
test2.cpp: In member function ‘void test::dummy()’:
test2.cpp:491:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&test::my_default’ [-fpermissive]
test2.cpp:491:17: error: cannot convert ‘void (test::*)(int)’ to ‘void (*)(int)’ in assignment
test2.cpp:496:77: error: invalid user-defined conversion from ‘test::dummy()::<lambda(int)>’ to ‘void (*)(int)’ [-fpermissive]
test2.cpp:496:28: note: candidate is: test::dummy()::<lambda(int)>::operator int (*)(int)() const <near match>
test2.cpp:496:28: note: no known conversion for implicit ‘this’ parameter from ‘int (*)(int)’ to ‘void (*)(int)’发布于 2014-01-28 20:34:30
问题是成员函数不是正常函数,它们不能分配给函数指针,因为它们是成员的类型是它们的类型的一部分。此外,成员函数需要有一个要调用的对象,该对象将是函数代码中的this。
您有几种解决方案:
解决方案2是最简单的std::function,用于处理与模板参数中的签名相同的任何可调用的签名。而且,它是唯一允许存储闭包(来自lambdas的对象)的解决方案。详情请参见C++11风格的回调?。
class test {
private:
// variable for function pointer
std::function< void ( int )> func;
// default output function
void my_default(int x) {
cout << "x =" << "\t" << x << endl << endl;
}
public:
void dummy(void) {
// 1. Test - default output function
cout << "my_default\n";
func = std::bind(&test::my_default, this, std::placeholders::_1);
// or
func = [&]( int i ){ my_default( i ); };
func(5);
// 2. Test - special output function 2
cout << "my_func2\n";
func = [](int x)->int{ cout << "x =" << " " << x << endl << endl; };
func(5);
}
};
// entry
int main() {
cout << "Test Programm\n\n";
test a;
a.dummy();
return 0;
}发布于 2014-01-28 20:39:18
成员函数与普通函数不同,因为必须有类的实例才能调用它(即将成为*this的对象)。不能使普通函数指针变量指向成员函数。
如果要创建一个可以使用类的任何实例调用的函数指针,则需要一个成员函数指针。你会写
void (test::*func)(int);宣布它,
func = &test::my_default;来分配它,并且
(this->*func)(5);叫它。当然,现在不能让成员函数指针指向lambda。
另一方面,如果您想将this绑定为实例,并从成员函数创建一个普通函数,那么您实际上无法创建一个普通函数指针。相反,您需要一个std::function对象,
std::function<void(int)> func;约束如下:
func = std::bind(&test::my_default, this, std::placeholders::_1);然后再正常打电话。std::function使用lambda就像函数指针一样。
https://stackoverflow.com/questions/21415862
复制相似问题