Can I use a lambda function or std::function object in place of a function pointer?

前端 未结 4 942
-上瘾入骨i
-上瘾入骨i 2021-02-13 21:04

I\'ve got a library that I need to use that defines the following:

typedef void CallbackFunction(const int& i);

and has a function to regis

4条回答
  •  甜味超标
    2021-02-13 21:26

    Since you capture nothing, you are supposed to be able to do what you're trying to do, namely assign a lambda expression as a function pointer (though your syntax is wrong there).

    Since you're using VS2010 though, you won't be able to. The feature of lambda that you're trying to use did not come to be until after VS2010 was released: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3043.html

    So while yes, it's supposed to work it in fact does not.

    Of course, while you are not, in fact, capturing anything you do state that you want to. Lambdas that capture data cannot be converted to function pointers even in the post VS2010 state of the C++ draft standard.

提交回复
热议问题