Template of the base variant of the Variable template: no parameters or one?

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

For example, I implement the sum function, taking an arbitrary number of arguments. Main template then

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

In the base case, I see at least two options:

  • Base Register - Amount ():

    auto sum() 
    { 
        return 0; 
    }
    
  • The base case is equal to the sum (T):

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

Both of these seem to work the same way in this case, but which one is usually preferable?

+4
source share
1 answer

The second case is more general than option 1, which introduces int.

.. sum , . ( , ).

int,

auto res = sum('*');

int 1 char 2

+4

Source: https://habr.com/ru/post/1598673/


All Articles