lundi 20 juillet 2015

Variadic function template base case: without parameters or with one?

I would like to know which one of these (if any) options would be preferred.

For example I'm implementing a sum function taking arbitrary number of arguments. The main template is then

template <typename T, typename... Ts>
auto sum(T t, Ts... ts)
{
    return t + sum(ts...);
}

For the base case I can see at least two options:

1.Base case is sum():

auto sum() 
{ 
    return 0; 
}

2.Base case is sum( T ):

template <typename T>
auto sum(T t)
{
    return t;
}

Both of those seem to work in the same way in this case, but which one would be generally preferred?

Aucun commentaire:

Enregistrer un commentaire