I am trying to figure out which is the most idiomatic way implement a function over a variadic type list. For example, computing the maximum size of all the types. I understand there exist several approaches to accomplish such a task, but I would like to know when to choose which strategy.
These are the mechanisms I would consider (there may exist more, please mention if so):
-
Type traits (ideally succinctly with using declarations):
template <typename Head> using max_size = typename std::integral_constant<size_t, sizeof(Head)>::type; template <typename Head, typename... Tail> using max_size = ?; -
constexprfunctions:template <typename Head> constexpr size_t max_size() { return sizeof(Head); } template <typename Head, typename... Tail> constexpr size_t max_size() { ? }
My question is twofold:
-
What features of the computation determine what strategy to choose?
-
In each case, how would an example implementation for the maximum size example described above look like?
I personally prefer functions rather than traits, I find them easier to manipulate and more natural. But that’s certainly subjective 😉
In action at liveworkspace: