需要帮助理解这篇关于模板的文章
Need help understanding this article about templates
我是从这个页面上阅读这篇文章的,所以让我展示一下代码:
template <class T> class D: typename C<T>::Base { //#1 error
D(): typename C<T>::Base(typename C<T>::Base(0)) { } //#2 error, #3 correct
typename C<T> f() { //#4 error
typename C<T>::A* p; //#5 correct
A<T>::B * n;
}
};
class Q{
typename C<long>::A * p; // #6 error
}
template <class T, class U> class R{
typename C<long>::A * p; // #7 optional
}
#3
是正确的,但我试图理解作者试图传达的内容。他说:
typename #3:正确。在这里,typename用于消除类型的歧义参数的类型。如果没有typename,表达式C::Base(0)将被视为对名为Base的函数的调用。与C::Base(0)创建一个类型的临时对象C::Base初始化参数为0。
同时,如果你看到这部分的上方,作者说:
typename关键字必须作为从属名称的前缀满足以下三个规则:
1。它出现在模板
中2。它是合格的//我根本无法理解这一行与这个引号的开始段的联系
3。它不用于基类规范或成员初始化列表。
我完全无法理解这一行(上面的#2)与这段引文的开头段的关系。你能解释一下作者的意思吗?
"Qualified"表示它不在同一作用域中,而是当前作用域中的子作用域,而当前作用域也依赖于模板。例如,
template <class T> struct C {
typedef int b;
}
b
在另一个模板中引用时是合格的id
,例如
template<typename T> struct M {
typename C<T>::b bb;
}
标准(§14.6.3)的相关引语:
引用类型的限定id,其中嵌套名称说明符依赖于模板形参(14.6.2)以关键字typename作为前缀,以指示限定id表示一个类型,形成一个详细的类型说明符(7.1.5.3)。
和§14.6.2:
A name used in a template declaration or definition and that is dependent on a template-parameter is
assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified
by the keyword typename. [Example:
// no B declared here
class X;
template<class T> class Y {
class Z; // forward declaration of member class
void f() {
X* a1; // declare pointer to X
T* a2; // declare pointer to T
Y* a3; // declare pointer to Y<T>
Z* a4; // declare pointer to Z
typedef typename T::A TA;
TA* a5; // declare pointer to T’s A
typename T::A* a6; // declare pointer to T’s A
T::A* a7; // T::A is not a type name:
// multiply T::A by a7; ill-formed,
// no visible declaration of a7
B* a8; // B is not a type name:
// multiply B by a8; ill-formed,
// no visible declarations of B and a8
}
};
—end example]
和§14.6.7:
Within the definition of a class template or within the definition of a member of a class template, the keyword
typename is not required when referring to the unqualified name of a previously declared member
of the class template that declares a type. The keyword typename shall always be specified when the
member is referred to using a qualified name, even if the qualifier is simply the class template name.
[Example:
template<class T> struct A {
typedef int B;
A::B b; // ill-formed: typename required before A::B
void f(A<T>::B); // ill-formed: typename required before A<T>::B
typename A::B g(); // OK
};
The keyword typename is required whether the qualified name is A or A<T> because A or A<T> are synonyms
within a class template with the parameter list <T>. ]
不确定这是否有帮助,但是一个模板使用的例子:
template<typename TemplateVar>
class SimpleTemplate
{
public:
TemplateVar Item;
void SetItem(TemplateVar &ItemCopy);
TemplateVar &GetItem();
};
template<typename TemplateVar>
void SimpleTemplate<TemplateVar>::SetItem(TemplateVar &ItemCopy)
{
Item = ItemCopy;
return;
}
template<typename TemplateVar>
TemplateVar &SimpleTemplate<TemplateVar>::GetItem()
{
return Item;
}
int main(int ArgC, char *ArgV[])
{
SimpleTemplate<char> Test;
return 0;
}
请随意对代码做任何您想做的事情(如果可以的话,甚至可以出售它)。
相关文章:
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 类成员函数参数列表是否可以依赖于模板参数?
- 依赖于模板的错误
- 将 c++ 类成员函数专用于模板类
- C++ 使函数调用依赖于模板参数
- c ++如何将模板应用于模板类的孩子/朋友?
- 如何修复"没有依赖于模板参数的参数'glGenVertexArrays'......"C++ 中的错误
- 如何转发声明依赖于变量定义的类,而变体定义又依赖于模板化类?
- 动态强制转换适用于模板成员函数内的共享指针和弱指针,无需代码重复
- 如何确保 "<<" 运算符适用于模板化 ADT 定义中的任何泛型类型?
- 将 const 限定符应用于模板参数时的不同编译器行为
- 如何定义依赖于模板参数的类型定义
- 如何使用类型专用化模板方法,该类型本身就是一个模板,其中只有返回类型依赖于模板类型
- PHP 相当于C++模板
- 仅专用于模板类的一个方法(部分)
- 依赖于模板的实现
- C++ 错误:"没有依赖于模板参数的'setw'参数
- 依赖于模板参数的范围查找
- 对应于模板中的定义的声明
- 使用的类型别名如何指定依赖于模板参数的模板模板参数