如何解决使用可变参数类模板的模板参数调用可变参数函数模板?
给定一个可变参数类模板,如何使用该类的模板参数调用可变参数函数模板?
示例:
template <typename T0,typename... Ts>
void test_variadic()
{
std::cout<<typeid(T0).name()<<std::endl;
if constexpr (sizeof...(Ts) > 0)
test_variadic<Ts...>();
}
template<typename T> // T is variadic class template
void f0()
{
// test_variadic<T...>(); // Call 'test_variadic' with underlying template parameter types of T? (In this example case `test_variadic<float,int>`)
}
template<typename ...T>
class Variadicclass
{};
int main(int argc,char *argv[])
{
f0<Variadicclass<float,int>>();
return EXIT_SUCCESS;
}
f0
的实现是我所缺少的,我希望它通过自动确定来自 test_variadic<float,int>
的模板参数列表并将其用于对 {{ 的调用来调用 T
1}}。我该怎么做?
test_variadic
但就我而言,我没有任何实际参数或对象,只有类型,因此 lambda 解决方案不起作用。
首选使用现代 C++ 的解决方案。
解决方法
您可以将具有部分特化的类模板声明为:
// primary template (might implement it with default behavior)
template <typename T> struct test_variadic_impl;
// partial specialization for variadic class template
template <template <typename...> typename C,typename... Args>
struct test_variadic_impl<C<Args...>> {
static auto call() {
return test_variadic<Args...>();
}
};
然后像这样使用它:
template<typename T> // T is variadic class template
void f0()
{
test_variadic_imple<T>::call();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。