Naturally, C++ compilers can inline function calls made from within a function template, when the inner function call is directly known in that scope (ref).
#include <iostream>
void holyheck()
{
std::cout << "!\n";
}
template <typename F>
void bar(F foo)
{
foo();
}
int main()
{
bar(holyheck);
}
Now what if I'm passing holyheck
into a class, which stores the function pointer (or equivalent) and later invokes it? Do I have any hope of getting this inlined? How?
template <typename F>
struct Foo
{
Foo(F f) : f(f) {};
void calledLater() { f(); }
private:
F f;
};
void sendMonkeys();
void sendTissues();
int main()
{
Foo<void(*)()> f(sendMonkeys);
Foo<void(*)()> g(sendTissues);
// lots of interaction with f and g, not shown here
f.calledLater();
g.calledLater();
}
My type Foo
is intended to isolate a ton of logic; it will be instantiated a few times. The specific function invoked from calledLater
is the only thing that differs between instantiations (though it never changes during the lifetime of a Foo
), so half of the purpose of Foo
is to abide by DRY. (The rest of its purpose is to keep this mechanism isolated from other code.)
But I don't want to introduce the overhead of an actual additional function call in doing so, because this is all taking place in a program bottleneck.
I don't speak ASM so analysing the compiled code isn't much use to me.
My instinct is that I have no chance of inlining here.