Saturday, October 11, 2008

c++: implicit typenames in templates

Template object is an incomplete type, for an obvious reason in some cases it's hard for compiler to decide what typename should be. Let's look at the this example

template<typename T>
class A
{
    public:
        struct B
        {
            T member;
        };
};

template<typename T>
class C
{
    public:
        A<T>::B member;
};
Your(and mine also ;]) compiler will probably tell you " type A<T> is not derived from type C<T>". A<T> is undefined, so A<T>::B is undefined also.
To make this work you have to tell compiler explicitly that A<T>::B is a typename.
template<typename T>
class A
{
    public:
        struct B
        {
            T member;
        };
};

template<typename T>
class C
{
    public:
        typename A<T>::B member;
};

No comments: