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

Call template function with typename combinations

I want to loop over a "list of typenames" and look at all combinations of them. I was thinking of using a tuple for this, compare:

template<typename T, typename R>
void foo() {
    std::cout << std::numeric_limits<T>::max() << ", " << std::numeric_limits<R>::max();
}

void call() {
    using int_types = std::tuple<int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t>;
    constexpr auto indizes = std::make_integer_sequence<size_t, 8>();
    for (auto i: indizes) {
        using T = std::tuple_element_t<i, int_types>;
        for (auto j: indizes) {
            using R = std::tuple_element_t<j, int_types>;
            foo<T, R>();
        }
    }
}

Of course for loops are not consteval, thus this approach does not work. I think it might work using recursion or some consteval stuff from the STL but I can’t quite figure it out.

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 :

You can expand pack that way

    using int_types = std::tuple<int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t>;
    
    []<typename... Ts>(std::tuple<Ts...>){
        ([]<typename T, typename... Us>(std::type_identity<T>, std::tuple<Us...>){
            (foo<T, Us>(), ...);
        }(std::type_identity<Ts>{}, int_types{}), ...);
    }(int_types{});

Demo

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