Recently, I've read a commit comment from range-v3 here: https://github.com/ericniebler/range-v3/commit/a4829172c0d6c43687ba213c54f430202efd7497
The commit message says,
marginally improve compile times by replacing std::forward with static_cast
I know that std::forward<T>(t)
returns static_cast<T&&>(t)
, by standard.
Also I do know sometimes static_cast<T&&>(t)
will work fine when T &&t
is universial referece(or forwarding reference) by reference collapsing rule.
What I'm interested in is the commit message says that static_cast
improves marginally compile performance. If std::forward<T>(t)
just returns static_cast<T&&>(t)
, what makes such differerence in compile performance?
Maybe does std::forward<T>(t)
require some kind of deduction anyway? Or, Does std::forward<T>(t)
do some magical thing which throttles compiler?
Every time you do std::forward<T>(t)
, the compiler has to instantiate several templates: std::forward
itself has two overloads, as well as remove_reference
which is part of the function's signature. And while these templates don't generate much code, it still has to do the work of template instantiation.
For most code, this would be essentially a rounding error in compile time. But for Ranges TS, with all of the forwarding of stuff they do there, it could be non-trivial (though still "marginal").