Robin Hsu
Robin Hsu

Reputation: 4484

c++ template variable code efficiency

Consider a template function (with primitive template variable, i.e. non-class, non-struct template variable):

#include <iostream>

template <int m, int n>
int f() {
     return m+n;
}

int main() {
    for (int i=0; i<2; ++i)
        for (int j=0; j<2; ++j)
            std::cout << f<i,j>() << endl;
}

Will the compiler generate 4 copies of f()? or Just 1 copy, with m, n as "internal" parameters?

UPDATE

It seems that this code can not compile. It seems that the template variable must be a constant. (I am not sure for this: any one?) If so, sorry for raising a ridiculous question.

Upvotes: 2

Views: 68

Answers (1)

user1978011
user1978011

Reputation: 3589

If your compiler was smart enough to deduce that the loops are constexpr, it would most likely unroll your loops and inline the function bodies.

However, when you try this with gcc, you get several errors like

test.cc:230:21: error: the value of 'i' is not usable in a constant expression
  std::cout << f < i, j > () << endl;

Non-constexpr template arguments are a problem, because templates need to be resolved at compile time, but how would a compiler resolve

std::cin >> a;
f< a, 2 >();

The compiler cannot make this compile-time decision and complains.


Apart from these obvious problems, I think your question was whether the compiler will emit several copies of the code for template instantiations with different arguments. In general, the answer is yes. However, modern compilers have clever ways to

  • check if the template arguments are equivalent and lead to the same code
  • fold binary code by identifying identical sections and deleting out all copies.

This blog from Honza Hubička, a gcc developer, gives a nice summary of gcc5's capabilities.

Upvotes: 2

Related Questions