Reputation: 4505
Using code from http://ideone.com/5MHVz I am curious how is it possible that I can bind a lambda function (inline) to a C style function pointer but I cannot do this with a class function even if there is no state involved. It must be some fundamental difference but I don't understand how lambda binding is possible in this case then (there is a conceptual this to lambda generated code also). Is there a workaround ?
Code bellow:
#include <iostream>
#include <functional>
using namespace std;
typedef int (*http_cb) (int*);
struct http_parser_settings {
http_cb on_message_begin;
};
class HttpParser
{
int OnMessageBegin(int* val){}
HttpParser()
{
http_parser_settings settings;
//settings.on_message_begin = std::bind(&HttpParser::OnMessageBegin, this, std::placeholders::_1); -- this one does not compile
settings.on_message_begin = [](int* p){ return 0;};
}
};
int main() {
}
Upvotes: 1
Views: 904
Reputation: 1872
You can store lambdas in std::function objects even in MSVC2010, so there must be a way to get a raw function pointer. I've never delved into the details of how it works, but have certainly used this feature before.
Upvotes: -1
Reputation: 476950
Non-capturing lambdas can be converted to function pointers. They're essentially free functions, so there's no problem.
Upvotes: 3