c – 为什么在这个类定义中不需要空参数列表?

当仅使用标准类型参数实例化具有模板化类类型的变量时,语法如下所示:
template<typename Arg = int>
class Templ;

Templ<>& myTempl;

省略空参数列表<>应该给出编译错误,因为需要模板参数列表.

但显然(至少在VS2013下),以下声明不需要模板参数列表:

template<typename Arg> //" = int" left out
class Templ{
    Templ& myTempl; //no <> here
};

但为什么这有效呢?根据IntelliSense,编译器选择了正确的类型(Templ< int>),因此它按预期工作,但成员声明是否仍然需要空参数列表?

编辑:不,它没有按预期工作.我没有仔细检查.当将鼠标悬停在Templ< short> :: myTempl行上时,IntelliSense会将其类型显示为较短.

解决方法

名称被注入类范围

9个班级[班级]

2 A class-name is inserted into the scope in which it is declared
immediately after the class-name is seen. The class-name is also
inserted into the scope of the class itself; this is kNown as the
injected-class-name
. For purposes of access checking,the
injected-class-name is treated as if it were a public member name. A
class-specifier is commonly referred to as a class deFinition. A class
is considered defined after the closing brace of its class-specifier
has been seen even though its member functions are in general not yet
defined. The optional attribute-specifier-seq appertains to the class;
the attributes in the attribute-specifier-seq are thereafter
considered attributes of the class whenever it is named.

类似于类模板

14.6.1本地声明的名称[temp.local]

1 Like normal (non-template) classes,class templates have an
injected-class-name
(Clause 9). The injectedclass- 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-typespecifier 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 <>.

这样您就可以使用Templ,意思是Templ< Arg>.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...