Conversion of lambda expression to function pointer

后端 未结 1 2082
失恋的感觉
失恋的感觉 2021-02-12 10:44

This is a follow up question to this question: Lambda how can I pass as a parameter

MSDN supposedly has marked the item as fixed. I took a look at the specifications, bu

相关标签:
1条回答
  • 2021-02-12 11:07

    There is no syntax per se, it's an implicit conversion. Simply cast it (explicitly or implicitly) and you'll get your function pointer. However, this was fixed after Visual Studio 2010 was released, so is not present.


    You cannot make a capture-full lambda into a function pointer ever, as you noted, so it's the function printOut that'll have to change. You can either generalize the function itself:

    // anything callable
    template <typename Func>
    void printOut(Func eval) 
    {
        // ...
    }
    

    Or generalize the function type in particular:

    // any function-like thing that fits the int(int) requirement
    void printOut(std::function<int(int)> eval) 
    {
        // ...
    }
    

    Each has their own trade-off.


    †As far as I know, it's unknown of we'll get it in a service pack, or if we need to wait until a new release.

    0 讨论(0)
提交回复
热议问题