Static_cast <T &&> (t) is faster than std :: forward <T> (t) for compilation?
I recently read a commit comment from range-v3 here: https://github.com/ericniebler/range-v3/commit/a4829172c0d6c43687ba213c54f430202efd7497
The commit message says:
slightly improve compile time by replacing std :: forward with static_cast
I know what std::forward<T>(t)
returns static_cast<T&&>(t)
as standard. Also sometimes I know it static_cast<T&&>(t)
will work fine if T &&t
is a generic referee (or a redirect link) using the link collapse rule.
What I need is a commit message that static_cast
improves minimal compilation. If it std::forward<T>(t)
just returns static_cast<T&&>(t)
, what makes such a difference in compilation performance?
Could it std::forward<T>(t)
require some kind of deduction? Or, does it do std::forward<T>(t)
any magic thing that throttles the compiler?
source to share
Each time you execute std::forward<T>(t)
, the compiler has to instantiate the template std::forward
(or at least check to make sure it created it using this T
). While this template doesn't generate a lot of code, it still has to do the work with an instance of the template.
For most code, this will essentially be a compile-time rounding error. But for the TS bands, with all the material they do there, it can be non-trivial (although still "marginal").
source to share