While reading [Compile-time code optimization] from wikipedia' article-Template metaprogramming:
template <int length>
Vector<length>& Vector<length>::operator+=(const Vector<length>& rhs)
{
for (int i = 0; i < length; ++i)
value[i] += rhs.value[i];
return *this;
}
When the compiler instantiates the function template defined above, the following code may be produced:[citation needed]
template <>
Vector<2>& Vector<2>::operator+=(const Vector<2>& rhs)
{
value[0] += rhs.value[0];
value[1] += rhs.value[1];
return *this;
}
The compiler's optimizer should be able to unroll the for loop because the template parameter length is a constant at compile time.
However, take caution as this may cause code bloat as separate unrolled code will be generated for each 'N'(vector size) you instantiate with.
However, I learned while writing TMP codes, loop should be avoided because it is run-time and recursion with templates is a substition.
I have search edit on google and find this question which unrool manually. Answers also encourage to use recursion.
So, should I rely on Compile-time code optimization which is able to unroll the for loop using compile-time determinate length(end of the loop) at compile time or use recursion all the time?
I think the article from wikepedia encourage us to rely on un-roll. Maybe I misunderstand it?