0

I want to pass lambda function with capture to another normal function f.

I have found only two alternatives :-

1. pass lambda function directly, using template

template<typename Lam> f(Lam lam,  something) {...}

Disadvantage: must be in header -> slower compile time

2. convert lambda to std::function

f(std::function<void(SomePointer*)> lam, something){...}

Disadvantage: execution cost is more (+100% for my case, optimized)

I want to pass many lambda functions that share the same signature to f (good for 2).
f is a complex function with long code. (also good for 2).

Question

Is there any technique that have advantage from both solutions?

This link Using lambda as an argument : std::function or template? vaguely said the answer is no.

Community
  • 1
  • 1
javaLover
  • 6,347
  • 2
  • 22
  • 67
  • @πάντα ῥεῖ I will interpret that, by marking that this question is duplicated, you confirmed the answer is "no" . :( – javaLover Aug 20 '16 at 08:45
  • 2
    There is a middle-way, between `std::function` and a specialization each: Look at http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible and http://codereview.stackexchange.com/questions/14730/impossibly-fast-delegate-in-c11 – Deduplicator Aug 20 '16 at 08:47
  • @Deduplicator The link does not work for lambda with capture, right? – javaLover Aug 20 '16 at 08:49
  • 1
    They work for any kind of object + member-function, be they a lambda or not. Of course, if your lambdas don't have captures, you could just convert to function-pointers. (BTW: Will try to add a better answer later. Need time for that.) – Deduplicator Aug 20 '16 at 08:49
  • @Deduplicator You second link is really good, thank a lot. – javaLover Aug 20 '16 at 09:06

0 Answers0