0

Bar::value() returns a float and is an override. _bar is a member Klass. Can the following be captured? If so what is the best approach with C++14?

void Klass::foo(void)
{
    using Expression = float (*)(void);

    Expression e2 = [](){return _bar.value();};
}

Edit:

From some example code I'm messing around with:

float Y::capture(void)
{
    auto e = [=](){return _x.junk();};

    Expression f = Expression(e);

    return capture(f);
}

G++ 4.9 complains:

error: invalid cast from type ‘Y::capture()::<lambda()>’ to type ‘Y::Expression {aka float (*)()}’
     Expression f = Expression(e);
Kenny
  • 675
  • 10
  • 24
  • So what exactly problem is? Not compiling? Not working? – Severin Pappadeux Oct 28 '15 at 02:10
  • G++ 4.9 is telling me that it cannot convert the lambda to an Expression. – Kenny Oct 28 '15 at 02:19
  • To capture members of class from inside you need to capture [this] – Andrey Lyubimov Oct 28 '15 at 02:26
  • Same error with [this]. It seems to allow the capture but it doesn't like to convert the lambda to a float (*)(void). – Kenny Oct 28 '15 at 02:28
  • 2
    I'm not sure but I don't think you can convert a lambda to a function pointer. It's more an anonymous class instance than a function. Yes, see the linked question – Claudiu Oct 28 '15 at 02:29
  • Ouch! I had some prototype code where my lambda was simply returning rand() and it convinced me that it simply thought of it as a pointer to function. – Kenny Oct 28 '15 at 02:34

0 Answers0