In general including stuff in fewer spots causes faster compilation.
However, template compilation slowdown mostly comes from it being used in multiple spots. Each time it is used, the template has to be instantiated, which can take much longer than it being parsed originally.
Moving the definition cannot change how many times it is instantiated, unless you also change other code.
If you want to know if build times will be faster with work, one way is to actually test it. Change:
template <typename Closure>
void Foo_util (Closure&& closure) {
Foo_fn1();
std::forward<Closure>(closure)();
}
to
void Foo_util (std::function<void()> closure);
and then in Foo.cpp
file:
void Foo::Foo_util(std::function<void()> closure) {
Foo_fn1();
closure();
}
almost any refactoring changes you make to your code will result in less speedup than the above.
(Note that a few lambdas won't convert to std function. If that is the case, here is a simple function view:
template<class Sig>
struct function_view;
template<class R, class...Args>
struct function_view<R(Args...)> {
template<class T,
std::enable_if_t<!std::is_same_v<std::decay_t<T>,function_view>, bool> = true
>
function_view( T&& t ):
pdata{std::addressof(t)},
paction(+[](state pt, Args&&...args)->R {
return ((T&&)(*static_cast<std::decay_t<T> const*>(pt.pdata)))(std::forward<Args>(args)...);
} )
{}
function_view( R(*pf)(Args...) ):
pdata{pf},
paction(+[](state pf, Args&&...args)->R {
return ((R(*)(Args...))pf.pfunc)(std::forward<Args>(args)...);
})
{}
R operator()(Args...args)const {
return paction(pdata, std::forward<Args>(args)...);
}
private:
union state {
const void* pdata;
R(*pfunc)(Args...);
state():pdata(nullptr){};
state(const void*p):pdata(p){}
state(R(*f)(Args...)):pfunc(f){}
};
state pdata;
R(*paction)(state, Args&&...) = nullptr;
};
used like this:
void Foo_util (function_view<void()> closure);
and then in Foo.cpp
file:
void Foo::Foo_util(function_view<void()> closure) {
Foo_fn1();
closure();
}