vendredi 25 août 2017

Is va_list still used in C++? Or is it encouraged to use template

In C, the only way to define a variable length of arguments is declaring its prototype with a ellipsis and use va_list, va_start, va_arg, va_end to extract them. Just like the printf series and the scanf series do.

In C++11, a new approach was introduced as below.

template <typename T, typename... MoreT>
void func(T arg, MoreT... args){
    // Do some stuff
    func(args);
}

What are the benefits and downsides of each method? Is either of them discouraged to be used or encouraged in C++?

Aucun commentaire:

Enregistrer un commentaire