sizeof 可变参数模板(所有元素的 sizeof 之和)

2022-01-20 00:00:00 tuples c++ c++11 sizeof variadic-templates

考虑以下功能:

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

为了返回 sizeof 所有参数的总和,最简单的方法是什么而不是 /* SOMETHING */ ?

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

例如 myFunction(int, char, double) = 4+1+8 = 13

推荐答案

unsigned myFunction() {return 0;}

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

相关文章