Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 833
Vincent Avatar asked Oct 01 '12 02:10

Vincent


1 Answers

In C++17, use a fold expression:

template<typename... List> 
inline constexpr unsigned int myFunction(const List&... list)
{
    return (0 + ... + sizeof(List));
}
like image 178
T.C. Avatar answered Sep 25 '22 06:09

T.C.