需要帮助理解这篇关于模板的文章

Need help understanding this article about templates

本文关键字:于模板 文章 帮助 助理      更新时间:2023-10-16

我是从这个页面上阅读这篇文章的,所以让我展示一下代码:

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;
}

请随意对代码做任何您想做的事情(如果可以的话,甚至可以出售它)。