Variadic templates – The sizeof… operator

The implementation of variadic templates includes a sizeof… operator that returns the number of template arguments in the parameter pack.

template<typename... Args> class Test
  size_t GetTCount()
    return sizeof...(Args);

// . . .

Test<int> data;
size_t args = data.GetTCount(); //1

Test<int, int, char*> data2;
args = data2.GetTCount(); //3

Test<int, float> data3;
args = data3.GetTCount(); //2

It’s really more of a count-of here but I guess they chose to reuse an existing operator that’s familiar to C++ developers.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s