Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic Templates and Type Traits

I currently have a variadic function which takes an arbitrary number of arguments of arbitrary types (duh), however, I want to restrict the types to ones which are POD only, and also the same size or smaller than that of a void*.

The void* check was easy, I just did this:

static_assert(sizeof...(Args) <= sizeof(PVOID), "Size of types must be <= memsize.");

However I can't work out how to do the same for std::is_pod.

Is this possible to do?

like image 529
RaptorFactor Avatar asked Feb 03 '23 17:02

RaptorFactor


1 Answers

You can write a meta-function to determine if all are POD types:

template <typename... Ts>
struct all_pod;

template <typename Head, typename... Tail>
struct all_pod<Head, Tail...>
{
    static const bool value = std::is_pod<Head>::value && all_pod<Tail...>::value;
};

template <typename T>
struct all_pod<T>
{
    static const bool value = std::is_pod<T>::value;
};

then

static_assert( all_pod<Args...>::value, "All types must be POD" );
like image 182
Peter Alexander Avatar answered Feb 05 '23 16:02

Peter Alexander