参数包是模板参数吗?

Is a parameter pack a template parameter?

cppreference写到模板参数包是模板参数:

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

这是真的吗?比如这样写对不对:

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

当然,把它想象成你在实例化MyClass时写下了一个类型序列(顺便说一句应该是template<typename... T> class MyClass;),然后这个序列被准确地复制到std::unique_ptr的实例化。 std::unique_ptr 最多接受两个参数,第二个参数比较特殊,所以并非所有参数都有效:

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;
}

是的,参数包是一个有效的模板参数,用于声明。但是当模板被 实例化 时,它会被替换为实际提供的模板列表 arguments,参见 https://en.cppreference.com/w/cpp/language/template_parameters

例如在您的示例中,MyClass<int> 将包含 std::unique_ptr<int>MyClass<int, MyDeleter> 将包含 std::unique_ptr<int, MyDeleter>,而 MyClass<int, MyDeleter, Foo> 将导致编译器错误 "wrong number of template arguments",因为 std::unique_ptr最多只能有两个。

除了其他的回答,我想补充一下这个问题从标准的角度回答

根据 [temp.variadic]/1:

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

是的,模板参数包就是模板参数。您发布的代码片段是正确的。