Connecting Tech Pros Worldwide Forums | Help | Site Map

typedef inherited from template spec.

Imre
Guest
 
Posts: n/a
#1: Sep 12 '06

Hi

Please consider the following code:

template <class T>
struct Base;

template <template <typenameclass 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


amparikh@gmail.com
Guest
 
Posts: n/a
#2: Sep 12 '06

re: typedef inherited from template spec.



Imre wrote:
Quote:
Hi
>
Please consider the following code:
>
template <class T>
struct Base;
>
template <template <typenameclass 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.

mlimber
Guest
 
Posts: n/a
#3: Sep 12 '06

re: typedef inherited from template spec.


Imre wrote:
Quote:
Hi
>
Please consider the following code:
>
template <class T>
struct Base;
>
template <template <typenameclass 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-lit...html#faq-35.18

and

http://womble.decadentplace.org.uk/c...disambiguation

Cheers! --M

Closed Thread


Similar C / C++ bytes