如何计算可变模板函数的参数个数?
即:
template<typename... T>
void f(const T&... t)
{
int n = number_of_args(t);
...
}
在上面实现number_of_args
的最佳方式是什么?
发布于 2021-02-26 20:04:17
#include <iostream>
template<typename ...Args>
struct SomeStruct
{
static const int size = sizeof...(Args);
};
template<typename... T>
void f(const T&... t)
{
// this is first way to get the number of arguments
constexpr auto size = sizeof...(T);
std::cout<<size <<std::endl;
}
int main ()
{
f("Raje", 2, 4, "ASH");
// this is 2nd way to get the number of arguments
std::cout<<SomeStruct<int, std::string>::size<<std::endl;
return 0;
}
https://stackoverflow.com/questions/12024304
复制相似问题