Vincent Vincent - 3 months ago 12
C++ Question

sizeof variadic template (sum of sizeof of all elements)

Considering the following function :

template<typename... List>
inline unsigned int myFunction(const List&... list)
{
return /* SOMETHING */;
}


What is the most simple thing to put instead of
/* SOMETHING */
in order to return the sum of
sizeof
all arguments ?

For example
myFunction(int, char, double) = 4+1+8 = 13

Answer
unsigned myFunction() {return 0;}

template <typename Head, typename... Tail>
unsigned myFunction(const Head & head, const Tail &... tail) {
    return sizeof head + myFunction(tail...);
}
Comments