如何检测可变参数模板中的第一个和最后一个参数?
如何检测可变参数模板中的第一个和最后一个参数?
对于第一个参数很简单(只需将 sizeof...(T)
与 0 进行比较),但是有没有办法检测最后一个元素?
例子:
#include <iostream>
#include <typeinfo>
template < class... T >
struct A
{
int foo(int k){ return k; };
};
template < class T1, class... T >
struct A< T1, T... >
{
A() :a()
{
std::cout<<"A i="<<sizeof...(T)<<std::endl
<<" a type = " << typeid(T1).name()<<std::endl;
}
int foo(int k){ return anotherA.foo( a.foo(k) ); };
T1 a;
A< T... > anotherA;
};
struct B1
{
B1(){ std::cout<<"b1"<<std::endl; };
int foo(int k){ std::cout<<"b1::foo() k="<<k<<std::endl; return k+1; };
};
struct B2
{
B2(){ std::cout<<"b2"<<std::endl; };
int foo(int k){ std::cout<<"b2::foo() k="<<k<<std::endl; return k+2; };
};
struct B3
{
B3(){ std::cout<<"b3"<<std::endl; };
int foo(int k){ std::cout<<"b3::foo() k="<<k<<std::endl; return k+3; };
};
int main ()
{
A< B3, B2, B1 > a;
std::cout<<"the value is "
<<a.foo(5)
<< std::endl;
}
How to detect the first and the last argument in the variadic templates?
For the 1st argument it is easy (just compare sizeof...(T)
with 0), but is there a way to detect the last element?
The example :
#include <iostream>
#include <typeinfo>
template < class... T >
struct A
{
int foo(int k){ return k; };
};
template < class T1, class... T >
struct A< T1, T... >
{
A() :a()
{
std::cout<<"A i="<<sizeof...(T)<<std::endl
<<" a type = " << typeid(T1).name()<<std::endl;
}
int foo(int k){ return anotherA.foo( a.foo(k) ); };
T1 a;
A< T... > anotherA;
};
struct B1
{
B1(){ std::cout<<"b1"<<std::endl; };
int foo(int k){ std::cout<<"b1::foo() k="<<k<<std::endl; return k+1; };
};
struct B2
{
B2(){ std::cout<<"b2"<<std::endl; };
int foo(int k){ std::cout<<"b2::foo() k="<<k<<std::endl; return k+2; };
};
struct B3
{
B3(){ std::cout<<"b3"<<std::endl; };
int foo(int k){ std::cout<<"b3::foo() k="<<k<<std::endl; return k+3; };
};
int main ()
{
A< B3, B2, B1 > a;
std::cout<<"the value is "
<<a.foo(5)
<< std::endl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是你想要的。但这里有两个名为
first
和last
的实用程序,它们分别采用可变参数模板和 typedef 作为第一个和最后一个类型:I'm not positive if this is what you want. But here are two utilities named
first
andlast
that take variadic templates and typedef the first and last type respectively:这是另一组带有便利函数
return_type
的代码,您可以使用它来访问可变模板列表中特定索引处的任何类型...然后您可以调整对return_type
的调用code> 以便获得第一个和最后一个参数(即,第一个参数位于 0,最后一个参数位于sizeof...(TypeList)
):这是一个示例使用便利功能实际代码中的
return_type
来确定可变参数模板中的第 N 个模板参数:在本例中,由于
return_type
的int
模板参数为2
,您将获得char
类型作为输出。任何超过2
的数字都会导致溢出,从而产生编译错误而不是运行时错误。如前所述,您可以对其进行调整,以便将其包装在结构中的函数内,该结构将允许您使用 sizeof...(TypeList) - 1 访问该特定结构实例的可变参数模板中的类型code> 应用于枚举。例如:Here's another set of code with a convenience function
return_type
that you could use to access any type at a specific index in a varadic template list ... you could then adapt the call toreturn_type
so that you get the first and the last arguments (i.e., the first argument will be at 0, and the last argument will be atsizeof...(TypeList)
):Here's an example of using the convenience function
return_type
in actual code to determine the Nth template argument in a variadic template:In this case, since the
int
template argument toreturn_type
is2
, you'll get thechar
type as the output. Any number over2
will cause an overflow that will create a compile rather than runtime error. As noted, you could adapt it so that it's wrapped inside a function in a structure that will allow you to access the types in the variadic template for that specific structure instance using thesizeof...(TypeList) - 1
applied to an enum. For instance: