Difference between typename parameter pack and auto parameter pack?

Viewed 76

Suppose I have this 2 definitions.

template<typename... Ts>
struct S{};

template<auto... Ts>
struct S{};

What's the difference between this 2 definitions?
When we should opt for the one or another?

2 Answers

They have different purpose, the former allows

S<int, char, float> s;

the later (only available since C++17) allows

S<42, '*', 42U> s;

Templates have 3 kinds of parameters:

  1. Template type parameters
  2. Template non-type parameters
  3. Template template parameters
template<typename... Ts>
struct S{};

Here Ts is a pack of type parameters. It accepts data types like in S<int, long>.

template<auto... Ts>
struct S{};

Here Ts is a pack of non-type parameters with a placeholder type. It accepts values like in S<12, 34L>.

auto allows mixing of values of different types.

template<int... Ts>
struct S{};

This is similar, but all values are of fixed type int.

Related