c++templatesstdtype-traits

Does std hold a type traits that transforms a list of types and aggregate each transformation value into a single expression?


With template specialization, it is possible to code a traits that takes a list of types, apply a transformation on each type that returns a boolean, and then evaluate a final expression that is a and on all transformation results:

#include <type_traits>

template<template<typename> class TRANSFO, typename ...ARGS>
struct Eval {};

template<template<typename> class TRANSFO, typename T, typename ...ARGS>
struct Eval<TRANSFO, T, ARGS...>
{
    static constexpr bool value = TRANSFO<T>::value and Eval<TRANSFO,ARGS...>::value;
};

template<template<typename> class TRANSFO>
struct Eval<TRANSFO>
{
    static constexpr bool value = true;
};

int main()
{
    static_assert (Eval<std::is_integral,int,long>::value == true);
    static_assert (Eval<std::is_integral,int,float,long>::value == false);
    static_assert (Eval<std::is_integral,float>::value == false);
}

Question does std have already such a thing ? Or is it possible to write it with traits existing in std ?


Solution

  • Question does std have already such a thing ? Or is it possible to write it with traits existing in std ?

    Nothing exactly like this, but the type_traits header can indeed remove the boilerplate with std::conjunction.

    template<template<typename> class TRANSFO, typename ...ARGS>
    using Eval = std::conjunction<TRANSFO<ARGS>...>;
    

    Check it out live at Compiler Explorer.