Question about dependent names and typename

Discussion in 'C++' started by Evan, Nov 28, 2006.

  1. Evan

    Evan Guest

    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.)
    Evan, Nov 28, 2006
    #1
    1. Advertising

  2. Evan

    Ondra Holub Guest

    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;
    };
    };
    Ondra Holub, Nov 28, 2006
    #2
    1. Advertising

  3. Evan

    Guest

    Evan wrote:
    > 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.
    But doesnt the above C<T>::Nested form the non deduced context, which
    means T cannot be deduced for these constructs ?

    >
    > 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.)
    , Nov 28, 2006
    #3
  4. Evan

    Evan Guest

    wrote:
    > Evan wrote:
    > > 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.
    > 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

    >
    > >
    > > 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.)
    Evan, Nov 28, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pervinder
    Replies:
    4
    Views:
    3,000
    Malte Starostik
    Mar 29, 2005
  2. Jakob Bieling

    Q: typename or not typename?

    Jakob Bieling, Mar 14, 2006, in forum: C++
    Replies:
    2
    Views:
    354
    Rolf Magnus
    Mar 14, 2006
  3. Sanil
    Replies:
    2
    Views:
    4,750
    James Kanze
    Dec 14, 2007
  4. puzzlecracker
    Replies:
    1
    Views:
    519
    James Kanze
    Aug 7, 2008
  5. Replies:
    3
    Views:
    270
    Zhihao Yuan
    Nov 20, 2012
Loading...

Share This Page