模板模板参数,为什么要强制类

Template template parameters, why is class forced?

本文关键字:为什么 参数      更新时间:2023-10-16

如果我写的话,滴定的滴滴几乎全部说明;

                                        vvvvv
template <template <typename, typename> class StdContainer, typename T>
class DerivedContainer: public StdContainer<T, std::allocator<T>>
{ /*...*/ };

为什么需要类关键字?那就是允许打字名称的时候,作为在所有其他模板上下文中的替换。歧义?另外标准状态的哪一部分?

注意;我不是从std容器中得出的,这只是一个例子。

在语法上要求 class声明模板参数时的使用情况:

n3337,§14.1[temp.param]:

1模板参数的语法是:

template-parameter:

type-parameter
parameter-declaration

type-parameter:

class ...opt identifier opt
class identifier opt = type-id
typename ...opt identifier opt
typename identifier opt= type-id
template < template-parameter-list > class ...opt identifier opt
template < template-parameter-list > class identifier opt = id-expression

我不知道这样做的确切原因。当您有一个简单的类型参数时,类型可以是从基本类型到用户声明的类,类模板的专业等。当它是模板模板参数时,它只能是类模板的名称。也许他们想强调这种差异 - 或者也许他们只是不愿意在语法中插入更多的线条并加剧了混乱。谁知道:)

class具有两个含义:声明类(或类模板)或声明模板类型参数。

typename可以代替第二个含义,但不能代替第一个含义。在模板模板参数中,您要声明类模板,因此您必须使用class