gpt4 book ai didi

c++ - Template模板参数,为什么要强制类?

转载 作者:太空狗 更新时间:2023-10-29 20:41:27 27 4
gpt4 key购买 nike

标题几乎说明了一切,如果我写的话;

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

为什么需要 class 关键字?那是允许 typename 的时候,作为所有其他模板上下文中的替换。消歧义?标准的哪一部分也说明了这一点?

注意;我不是从 std 容器派生的,这只是一个例子。

最佳答案

关键词class的用法当语法要求声明模板模板参数时:

n3337, §14.1 [temp.param]:

1 The syntax for template-parameters is:

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

我不知 Prop 体的原因。当你有一个简单的类型参数时,类型可以是从基本类型到用户声明的类、类模板的特化等任何类型。当它是模板模板参数时,它只能是类模板<的名称/em>。也许他们想强调这种差异 - 或者也许他们只是懒得在语法中插入更多行并增加困惑。谁知道:)

关于c++ - Template模板参数,为什么要强制类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333990/

27 4 0