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

How to extract type list from tuple for struct/class

I want to use a static method in a class that gets a type list in the form std::tuple<T1, T2, T3,...>. Instead of working with std::tuple<...> I want to have <...>.
How to implement example struct x resulting in Ts == <T1, T2, T3,...>

template<template <typename...> typename TL, typename... Ts>
struct x {
    static void test() {
        // expecting Ts == <int, char, float, double> (without std::tuple<>)
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }
};
using types = std::tuple<int, char, float, double>;
x<types>::test();

See example on godbolt

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 :

It seems to me that you’re looking for template specialization.

Something as

// declaration (not definition) for a template struct x
// receiving a single template parameter 
template <typename>
struct x;

// definition for a x specialization when the template
// parameter is in the form TL<Ts...>
template<template<typename...> typename TL, typename... Ts>
struct x<TL<Ts...>> {
    static constexpr void test() {
        // you can use Ts... (and also TL, if you want) here
        std::cout << __PRETTY_FUNCTION__ << ' ' << sizeof...(Ts) << '\n';            
    }
};
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