I had a look at the dump files from gcc to see how the compiler deals with recursive variadic template functions.
With this test code,
#include <cstdio>
#include <cstdarg>
using namespace std;
int sum(int n, ...)
{
va_list arguments;
va_start(arguments, n);
int sum = 0;
int argument = n;
while (argument != 0)
{
sum += argument;
argument = va_arg(arguments, int);
}
return sum;
}
int sum2(int n)
{
return n;
}
template<typename... Arguments>
int sum2(int n, Arguments... arguments)
{
return n + sum2(arguments...);
}
int main()
{
printf("%i %i\n", sum(1, 2, 3, 4, 0), sum2(1, 2, 3, 4));
}
compiled with,
gcc test.cpp -std=c++11 -fdump-rtl-all
dumps,
;; Function int sum(int, ...) (_Z3sumiz, funcdef_no=0, decl_uid=2538, cgraph_uid=0)
;; Function int sum2(int) (_Z4sum2i, funcdef_no=1, decl_uid=2547, cgraph_uid=1)
;; Function int sum2(int, Arguments ...) [with Arguments = {int, int, int}] (_Z4sum2IIiiiEEiiDpT_, funcdef_no=4, decl_uid=2557, cgraph_uid=3)
;; Function int sum2(int, Arguments ...) [with Arguments = {int, int}] (_Z4sum2IIiiEEiiDpT_, funcdef_no=5, decl_uid=2564, cgraph_uid=5)
;; Function int sum2(int, Arguments ...) [with Arguments = {int}] (_Z4sum2IIiEEiiDpT_, funcdef_no=6, decl_uid=2574, cgraph_uid=7)
So the compiler has generated 3 separate functions for each number of arguments during a recursive call.
Well, with full optimization, such primitive cases like this gets optimized away without even a function call, but can I still rely on the compiler to do such optimization even in complicated cases? or are recursive template functions still susceptible to code bloat when things get more complicated?
Aucun commentaire:
Enregistrer un commentaire