93

How can I get a count of the number of arguments to a variadic template function?

ie:

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

    ...
}

What is the best way to implement number_of_args in the above?

Andrew Tomazos
  • 58,923
  • 32
  • 156
  • 267

2 Answers2

111

Just write this:

const std::size_t n = sizeof...(T); //you may use `constexpr` instead of `const`

Note that n is a constant expression (i.e known at compile-time), which means you may use it where constant expression is needed, such as:

std::array<int,   n>  a; //array of  n elements
std::array<int, 2*n>  b; //array of (2*n) elements

auto middle = std::get<n/2>(tupleInstance);

Note that if you want to compute aggregated size of the packed types (as opposed to number of types in the pack), then you've to do something like this:

template<std::size_t ...>
struct add_all : std::integral_constant< std::size_t,0 > {};

template<std::size_t X, std::size_t ... Xs>
struct add_all<X,Xs...> : 
  std::integral_constant< std::size_t, X + add_all<Xs...>::value > {};

then do this:

constexpr auto size = add_all< sizeof(T)... >::value;

In C++17 (and later), computing the sum of size of the types is much simpler using fold expression:

constexpr auto size = (sizeof(T) + ...);

Hope that helps.

Pharap
  • 3,161
  • 4
  • 31
  • 44
Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • 9
    +1 Learned two things; `sizeof...` and `constexpr`. :) – Qix - MONICA WAS MISTREATED Jun 18 '14 at 08:38
  • 2
    So this `sizeof...` actually returns the number of arguments and not the combined storage size of all arguments (like `sizeof` on an array would)? – panzi Aug 09 '14 at 02:47
  • @panzi: Yes. `sizeof ...(T)` returns *number* of types packed in `T`. If you want to compute aggregated size of the packed types, then you've to do something like this: http://ideone.com/udggBk I've added this in my answer as well. – Nawaz Aug 09 '14 at 06:07
  • @panzi: the computation in my answer is slightly improved now. – Nawaz Aug 09 '14 at 06:15
  • @Nawaz No that is what I want. I just was suspicious because of the different behavior of `sizeof`. Thanks. :) – panzi Aug 09 '14 at 12:58
  • 3
    With C++17, to calculate size of individual arg types now we can use fold expressions `return (0 + ... + sizeof(t));` – P0W Nov 07 '16 at 09:47
  • @P0W: Yes. Thanks. I'll add that to my answer once C++17 is out. – Nawaz Nov 07 '16 at 10:46
-1
#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;
}