开发者

Templates :Name resolution -->IS this statement is true while inheritance?

This is the statement from ISO C++ Standard 14.6/6:

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>. ]

Is this statement is true while inheritance?

If yes, can anyone explain this?

I checked with inner class; it is accepted? But I am unable to check with inheritance?


Yes, that is equally true of inherited members.

The keyword typename is required for members of base templates, but not base classes in general. The reason it is required for base templates is that their members are not automatically brought into the scope of the class {} block, so the only way to refer to them is with a qualified-id, which requires typename.

template< typename >
class base1
    { typedef int type1; };

class base2 
    { typedef int type2; };

template< typename A >
class derived
    : base1< A >, base2 {
    typename base1< A >::type1 x;
    type2 y;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜