typedef inherited from template spec.



 DEVELOP > c-Plus-Plus > typedef inherited from template spec.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Imre"
Date: 12 Sep 2006 10:58:22 AM
Object: typedef inherited from template spec.
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
.

User: ""

Title: Re: typedef inherited from template spec. 12 Sep 2006 12:19:01 PM
Imre wrote:

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

User: "mlimber"

Title: Re: typedef inherited from template spec. 12 Sep 2006 12:41:55 PM
Imre wrote:

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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER