首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C++11:可变模板函数参数个数?

C++11:可变模板函数参数个数?
EN

Stack Overflow用户
提问于 2012-08-19 12:50:37
回答 1查看 38.1K关注 0票数 96

如何计算可变模板函数的参数个数?

即:

template<typename... T>
void f(const T&... t)
{
    int n = number_of_args(t);

    ...
}

在上面实现number_of_args的最佳方式是什么?

EN

回答 1

Stack Overflow用户

发布于 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;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12024304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档