Search code examples
c++variadic-templatesconstexpr

Is there a way of having a variable number of typenames in a template function, like parameter pack?


I'm on a microcontroller, so in order to avoid the new operator, I reserve some memory to be used in a placement new operator call. I'd like to determine the minimum size of that memory by supplying a number of types to a constexpr function that will return the largest type in the list.

struct A
{
   int foo;
   int bar[24];
};
struct B
{
   int foo;
   int bar[126];
};


uint8_t objectMem[max_sizeof<A, B>());

Is there such a thing as a "parameter pack" for typenames? I'd like to be able to append any number of types as template arguments.


Solution

  • Something along these lines:

    template <typename... Ts>
    constexpr size_t max_sizeof() {
      return sizeof(std::aligned_union<0, Ts...>::type);
    }