beginpluses
beginpluses

Reputation: 537

Is a parameter pack a template parameter?

cppreference writes that a template parameter pack is a template parameter:

https://en.cppreference.com/w/cpp/language/parameter_pack

Is it true? For example, is it correct to write something like this:

template<typename... Ts>
class MyClass {
    std::unique_ptr<Ts...> m_data;
};

Upvotes: 2

Views: 209

Answers (3)

L. F.
L. F.

Reputation: 20569

In addition to the other answers, I would like to add an answer to this question from the perspective of the standard.

Per [temp.variadic]/1:

A template parameter pack is a template parameter that accepts zero or more template arguments.

So yeah, a template parameter pack is a template parameter. The code snippet you posted is correct.

Upvotes: 0

aparpara
aparpara

Reputation: 2201

Yes, a parameter pack is a valid template parameter, to use in a declaration. But when a template is instantiated, it is replaced with a list of actually supplied template arguments, see https://en.cppreference.com/w/cpp/language/template_parameters

E.g. in your example MyClass<int> will contain std::unique_ptr<int>, MyClass<int, MyDeleter> will contain std::unique_ptr<int, MyDeleter> and MyClass<int, MyDeleter, Foo> will cause a compiler error "wrong number of template arguments", because std::unique_ptr may have at most two of them.

Upvotes: 1

bipll
bipll

Reputation: 11940

Sure, think of it as if you wrote down a sequence of types when instantiating MyClass (should be template<typename... T> class MyClass; btw), and then this sequence was accurately copied into std::unique_ptr's instantiation. std::unique_ptr accepts up to two parameters, and the second one is something special, so not everything would work:

int main() {
        MyClass<int> this_compiles;
        MyClass<int, std::default_delete<int>> as_does_this;
        //MyClass<int, double, char> this_does_not;
        //MyClass<> this_neither;
}

Upvotes: 0

Related Questions