如何将 C++ 类型包装到可变参数列表中

How to wrap C++ types into a list for variadic arguments

与我之前的一个问题有关。

刚开始使用折叠表达式,但它的行为还没有达到我的预期。 背景是我希望能够在专门的头文件中定义'my_list'来简化维护。使用不同类型的调用有效,但使用 'my_list' 调用时无效。 请参阅示例中的注释。

有没有办法让第二种调用类型起作用?

template < typename ... Types > struct tl
{
};
using my_list = tl <int, float, uint64_t>;


    template <typename ... Types>
void myFunc2() 
{
    (std::cout << "Size: " << sizeof (Types) << std::endl, ...);     
} 

main () 
{
    myFunc2<int,uint64_t,bool,uint16_t>();  // This call prints size of each type
    myFunc2<my_list>();                     // This call only prints 1
} 

您需要专注于您的类型列表(或任何类型列表)。使用函数对象变量模板,这将是:

template <typename ... Types>
static constexpr auto myFunc2 = [] {
    (std::cout << "Size: " << sizeof (Types) << std::endl, ...);     
};

template <template<class...> class TL, typename ... Types>
static constexpr auto myFunc2<TL<Types...>> = [] {
    (std::cout << "Size: " << sizeof (Types) << std::endl, ...);     
};

Example.