Question about dependent names and typename

E

Evan

Is the following code legal C++?

template <class T >
class C {
class Nested {
C<T>::Nested *n; // typename???
};
};

My understanding was that C<T>::Nested should be considered a dependent
name so should have typename before it. Both MSVC and GCC fail with
errors like "missing ; before *" which seems to confirm my suspicions.
However, Comeau's online C++ compiler lets it pass successfully, which
makes me question the correctness considering Comeau's reputation as
being compliant.

So is this a quirk of Comeau that they let this through because it's
commonly (mis-)used idiom, or is this GCC and MSVC failing to let
through something they should? Or is the standard ambiguous and they
are just interpreting it differently?

(Comeau also lets through the code with a typename. But I also thought
that in all cases typename was either required or prohibited (so never
optional), which would seem to say that Comeau has to be wrong one way
or the other.)
 
O

Ondra Holub

Evan napsal:
Is the following code legal C++?

template <class T >
class C {
class Nested {
C<T>::Nested *n; // typename???
};
};

My understanding was that C<T>::Nested should be considered a dependent
name so should have typename before it. Both MSVC and GCC fail with
errors like "missing ; before *" which seems to confirm my suspicions.
However, Comeau's online C++ compiler lets it pass successfully, which
makes me question the correctness considering Comeau's reputation as
being compliant.

So is this a quirk of Comeau that they let this through because it's
commonly (mis-)used idiom, or is this GCC and MSVC failing to let
through something they should? Or is the standard ambiguous and they
are just interpreting it differently?

(Comeau also lets through the code with a typename. But I also thought
that in all cases typename was either required or prohibited (so never
optional), which would seem to say that Comeau has to be wrong one way
or the other.)

I think there should be typename. Anyway, I would write it as

template <class T >
class C
{
class Nested
{
Nested *n;
};
};
 
A

amparikh

Evan said:
Is the following code legal C++?

template <class T >
class C {
class Nested {
C<T>::Nested *n; // typename???
};
};

You definitely need a typename for sure.
 
E

Evan

You definitely need a typename for sure.
But doesnt the above C<T>::Nested form the non deduced context, which
means T cannot be deduced for these constructs ?

I have no clue what this even means. :)

This situation arose because I'm trying to do source-to-source
transformation of C++, and the tool that I'm doing is very buggy. (I
think that this is the first time that the pretty-printing feature has
really been exercised; it's fairly new, and the previous work that's
been done one it has been analyses, so no output required.) A version
of the above where the "C<T>::" was removed (to give what the other
poster suggested, and what most people would write) currently outputs
as the above. I needed to find out if this was an error, partly so I
know how to fix it but mostly just for curiosity.

Thanks for your response (and to the other person who replied and
anyone else who does so in the future with more clarifications or
anything)

Evan
 

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
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top