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

时间:2011-09-02 17:15:17

标签: c++ templates

我正在阅读本文page中的这篇文章,所以让我提出代码:

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的函数的调用。随着   typename前缀,C :: Base(0)创建一个类型的临时对象   C :: Base使用参数0初始化。

另外,如果您在该部分上方看到一点,作者说:

  

typename关键字必须在该名称前面加上一个从属名称   满足以下三条规则:

     

1.它出现在模板中

     

2. 这是合格的//我完全无法理解这一行与此引语的起始段

     

3.它不用于基类规范或成员初始化   列表。

我根本无法理解这一行(上面的#2)以及此引文的起始段落。你能解释一下作者的意思吗?

2 个答案:

答案 0 :(得分:2)

“合格”意味着它不在同一范围内,而是当前范围的子范围,也取决于模板。例如,

template <class T> struct C {
    typedef int b;
}

b在另一个模板中引用时是合格的id,例如

template<typename T> struct M {
    typename C<T>::b bb;
}

标准的相关引用(§14.6.3):

  

引用类型的限定ID,其中包含   nested-name-specifier依赖于template-parameter(14.6.2)   以关键字typename为前缀以指示qualified-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>. ]

答案 1 :(得分:0)

不确定这是否有帮助,但是模板使用的示例:

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

随意使用代码做任何你想做的事情(如果可以的话,甚至卖掉它)。