typedef inherited from template spec.

I

Imre

Hi

Please consider the following code:

template <class T>
struct Base;

template <template <typename> class T, typename P>
struct Base<T<P> >
{
typedef int Type;
};

template <typename P>
struct Derived:
public Base<Derived<P> >
{
Type v1;
typename Derived<P>::Type v2;
};

For some reason, the declaration of Derived::v1 doesn't compile, while
the declaration of v2 does, and I can't really understand why. I tested
it with VC++ 8.1 and the online Comeau compiler, the results were the
same.

What's the difference between Type and Derived<P>::Type inside
Derived<P>?

Some additional observations:
1. If I add a typedef int Type; into the primary Base template, then it
compiles with VC++ 8.1, but still won't compile with Comeau. I don't
see how this should change anything (the primary template is still not
used at all), so Comeau's behavior seems to make more sense.
2. If I remove the specialization, both compilers compile this code.

So, could someone explain this to me?
Thanks,

Imre
 
A

amparikh

Imre said:
Hi

Please consider the following code:

template <class T>
struct Base;

template <template <typename> class T, typename P>
struct Base<T<P> >
{
typedef int Type;
};

template <typename P>
struct Derived:
public Base<Derived<P> >
{
Type v1;
typename Derived<P>::Type v2;
};

For some reason, the declaration of Derived::v1 doesn't compile, while
the declaration of v2 does, and I can't really understand why. I tested
it with VC++ 8.1 and the online Comeau compiler, the results were the
same.

What's the difference between Type and Derived<P>::Type inside
Derived<P>?

Some additional observations:
1. If I add a typedef int Type; into the primary Base template, then it
compiles with VC++ 8.1, but still won't compile with Comeau. I don't
see how this should change anything (the primary template is still not
used at all), so Comeau's behavior seems to make more sense.
2. If I remove the specialization, both compilers compile this code.

So, could someone explain this to me?
Thanks,

Imre

Look up ' dependent names". It is also covered in the FAQ.
 
M

mlimber

Imre said:
Hi

Please consider the following code:

template <class T>
struct Base;

template <template <typename> class T, typename P>
struct Base<T<P> >
{
typedef int Type;
};

template <typename P>
struct Derived:
public Base<Derived<P> >
{
Type v1;
typename Derived<P>::Type v2;
};

For some reason, the declaration of Derived::v1 doesn't compile, while
the declaration of v2 does, and I can't really understand why. I tested
it with VC++ 8.1 and the online Comeau compiler, the results were the
same.

What's the difference between Type and Derived<P>::Type inside
Derived<P>?

Some additional observations:
1. If I add a typedef int Type; into the primary Base template, then it
compiles with VC++ 8.1, but still won't compile with Comeau. I don't
see how this should change anything (the primary template is still not
used at all), so Comeau's behavior seems to make more sense.
2. If I remove the specialization, both compilers compile this code.

So, could someone explain this to me?

See these FAQs:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

and

http://womble.decadentplace.org.uk/c++/template-faq.html#disambiguation

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top