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

|
Related Articles |
|
|