gpt4 book ai didi

c++ - 在构造函数中为类名添加和省略模板参数之间的区别

转载 作者:行者123 更新时间:2023-11-30 03:36:43 25 4
gpt4 key购买 nike

我想知道两者之间是否存在本质区别:

template <typename T> 
class foo{
foo<T>(){};
};

template<typename T>
class foo{
foo(){};
};

两者似乎都有效,但我不明白两者之间的区别。第一个让我很困惑,我不明白这里的 < T > 的作用是什么。

最佳答案

根据injected-class-name的规则,它们完全是同一回事。

$14.6.1/1 Locally declared names [temp.local] :

Like normal (non-template) classes, class templates have an injected-class-name (Clause [class]). The injected-class-name can be used as a template-name or a type-name. When it is used with a template-argument-list, as a template-argument for a template template-parameter, or as the final identifier in the elaborated-type-specifier of a friend class template declaration, it refers to the class template itself. Otherwise, it is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

所以 foofoo<T>在这里指的是同样的事情。更具体地说,

The first one is confusing me, I don't understand what's the role of the < T > here.

您正在使用注入(inject)类名 foo及其模板参数 T (即 foo<T> ),它指的是模板类本身。

关于c++ - 在构造函数中为类名添加和省略模板参数之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40563997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com