Let's say I have some lambdas with exactly the same captures and exactly the same signatures.
int captured;
auto l0 = [&captured](int x){ captured += x; };
auto l1 = [&captured](int x){ captured -= x; };
auto l2 = [&captured](int x){ captured = x + 1; };
Now, let's say I need to store these lambdas in an std::vector
, to call them at runtime.
I cannot use a raw function pointer, as the captured variable forces the lambda to be a functor, rather than a conventional function.
I could use std::function
, but it's overkill, since I know for sure that all the lambdas have the same signature and the same captures. Since std::function
supports lambdas with same signature but different captures, I'm (very probably) paying an additional runtime cost that could be (?) avoided.
std::vector<decltype(l0)> v0; // Ok
v0.emplace_back(l0); // Ok
v1.emplace_back(l1); // Nope: `decltype(l0) != decltype(l1)`
v2.emplace_back(l2); // Nope: `decltype(l0) != decltype(l2)`
I would like something to find out a common type between all lambdas, but std::common_type
does not work.
// Nope: does not compile
using LCT = std::common_type_t<decltype(l0), decltype(l1), decltype(l2)>;
Basically, I need something between a raw function pointer and std::function
. Does anything like that exist? And... can anything like that be actually implemented?
Aucun commentaire:
Enregistrer un commentaire