0

Im doing a bit crazy project so dont get scared Im creating some kind of lambda calculus in CPP. Anyway how do I make it work?

auto INC_ = [=](void *arg, void *(*n)(void*, void*))-> void * {return n(arg,arg); };
auto do_ = [=](void *arg1, void *arg2)-> void * {return arg1; };
INC(a,do_);

INC_ gets 2 arg however second arg is a lambda exp with 2 other arguments.

do_ do what must be done with 2 arg

However if I try to call it all together like in 3 line. I get wrong arguments error.

So the real question is how to pass 2 arg lambda so it executes properly?

7
  • 1
    What is with all the void*s? C++ has a type system that is pretty useful. Commented May 9, 2017 at 18:07
  • I get wrong arguments error. And, exact error text is.. What, exactly? Commented May 9, 2017 at 18:08
  • Im required to get as close as possible to typeless code. I wrote at the beginning that its a bit crazy ;) If you can show example on int i can try to copy it into fancy void* Commented May 9, 2017 at 18:10
  • 1
    Ah. If you want *typeless" code you should use generic code (templates). See this reworked example: coliru.stacked-crooked.com/a/7d3d1c246a9a0564 Commented May 9, 2017 at 18:12
  • "Function cant be called with the given arg list" ;) And more after comp: error C2064: term does not evaluate to a function taking 2 arguments note: class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments Commented May 9, 2017 at 18:12

1 Answer 1

1

A lambda is not a function pointer.

auto INC_ = [=](void *arg, void *(*n)(void*, void*))-> void * {return n(arg,arg); };
auto do_ = [=](void *arg1, void *arg2)-> void * {return arg1; };
INC(a,do_);

The second argument to INC_ is a function pointer.

A stateless lambda can be converted to a function pointer with compatible signature.

Once there is state, this is not possible. [=] makes it stateful, even though it doesn't capture anything.

In addition, don't use function pointers in typeless code.

Try this:

int a = 0;
auto INC_ = [](auto arg, auto n) {return n(arg,arg); };
auto do_ = [](auto arg1, auto arg2) {return arg1; };
std::cout << INC(a,do_) << "\n";

live example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.