Obtaining function pointer to lambda?

前端 未结 3 1203
野的像风
野的像风 2020-12-23 16:35

I want to be able to obtain a function pointer to a lambda in C++.

I can do:

int (*c)(int) = [](int i) { return i; };

And, of cours

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 17:17

    This fails:

    auto *b = [](int i) { return i; };
    

    because the lambda is not a pointer. auto does not allow for conversions. Even though the lambda is convertible to something that is a pointer, that's not going to be done for you - you have to do it yourself. Whether with a cast:

    auto *c = static_cast([](int i){return i;});
    

    Or with some sorcery:

    auto *d = +[](int i) { return i; };
    

提交回复
热议问题