Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading