My question is the inverse of Why can lambdas be better optimized by the compiler than plain functions? The accepted answer says
The reason is that lambdas are function objects so passing them to a function template will instantiate a new function specifically for that object. The compiler can thus trivially inline the lambda call.
Thus, the question is What circumstances would cause a compiler to not inline a lambda passed to an inline function template? Consider the following setup:
template <typename F>
static inline void
hof(const F fun) {
...
fun(a, b, c); // a, b, c are int.
...
}
void
caller() {
...
hof([&](int a, int b, int c) { ... });
...
}
Further assume a recent gcc or clang with all relevant optimization flags turned on.
The question (which is in the form of a challenge) is to fill in the ...
parts with code so that the compiler fails to inline either the call to hof
or the call to fun
. You may use loops to call fun
multiple times or whatever (but only one call to hof
).
My claim is that (excluding "funny business" like exceptions, longjmp
, reflection, etc) it can't be done. Please try and prove me wrong. I'll accept any answer for which I can verify using godbolt.org that the lambda isn't inlined.