Topic: template name resolution


Author: "TiTi" <TJunkMyAss@mad.scientist.com>
Date: 1999/10/15
Raw View

> The reason is, there can exist a specialization of the template class A
> where value_type is not a type name. By default, the compiler assumes
> that such template members are non-types. To tell the compiler that they
> are types, use the 'typename' keyword.


What do you mean by "there can exist a specialization of template class A
where value_type is not a type name". And if there is such a specialization,
can you give us an example?



TiTi.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Matt McClure <matthew.mcclure.es.99@aya.yale.edu>
Date: 1999/10/15
Raw View
Date: 15 Oct 1999 15:59:49 GMT
From: "TiTi" <TJunkMyAss@mad.scientist.com>

> > The reason is, there can exist a specialization of the template class A
> > where value_type is not a type name. By default, the compiler assumes
> > that such template members are non-types. To tell the compiler that they
> > are types, use the 'typename' keyword.
>
>
> What do you mean by "there can exist a specialization of template class A
> where value_type is not a type name". And if there is such a specialization,
> can you give us an example?
>

There was no such specialization in my initial example, but I think this
is what Biju means:

   template <class T> class A {
   public:
      typedef T value_type;
      value_type v;
   };

   // specialization with no member value_type
   template <> class A<int> { };

   // specialization with value_type not a type name
   template <> class A<char> {
      int value_type;
   };

Neither A<int> nor A<char> has a type named value_type.


--
                --------------------------------------
                | matthew.mcclure.es.99@aya.yale.edu |
                |  http://www.faradic.net/~mmcclure  |
                --------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/10/15
Raw View
TiTi wrote:
>
> > The reason is, there can exist a specialization of the template class A
> > where value_type is not a type name. By default, the compiler assumes
> > that such template members are non-types. To tell the compiler that they
> > are types, use the 'typename' keyword.
>
> What do you mean by "there can exist a specialization of template class A
> where value_type is not a type name". And if there is such a specialization,
> can you give us an example?

Easy:

template<> class A<int>
{
  int value_type; // for A<int>, value_type is a member variable
};

template<class T> class A<T*>
{
  void value_type(); // for all A<T*>, value_type is a function
};


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Matt McClure <matthew.mcclure.es.99@aya.yale.edu>
Date: 1999/10/15
Raw View
Date: 14 Oct 99 16:36:50 GMT
From: Biju Thomas <b_thomas@ibm.net>

> Matt McClure wrote:
> >
> > In the following example, when A<T>::value_type is used as a type within
> > a function body (f1(), f1_2()), it is successfully looked up as such.
> > However, when it is used as a return type or as parameter type, it is
> > not.
> >
> >    template <class T> class A {
> >    public:
> >       typedef T value_type;
> >       value_type v;
> >    };
> >
> >    template <class T>
> >    void f1(A<T> a) {
> >       A<T>::value_type v = a.v;
> >    }
> >
> >    template <class T>
> >    void f1_2() {
> >       A<T>::value_type v;
> >    }
> >
> >    template <class T>
> >    A<T>::value_type          // error: expected a type specifier
> >    f2(A<T> a) {
> >       return a.v;
> >    }
> >
> >    template <class T>
> >    void
> >    f3(A<T>::value_type v) {  // error: expected a type specifier
> >
> >    }
> >
> >    template <class T>
> >    A<T>::value_type          // error: expected a type specifier
> >    f4(A<T>::value_type v) {  // error: expected a type specifier
> >       return v;
> >    }
> >
> > Should all uses of A<T>::value_type generate an error?
>
> Yes. To prevent the error, prefix it with the 'typename' keyword.

So, is it a compiler bug that the cases f1() and f1_2() do not generate
an error, or is there some other rule in the standard that allows
A<T>::value_type to be interpreted as a type in these cases (when the
type occurs in the body of the function rather than the return type or
parameters)?

--
                --------------------------------------
                | matthew.mcclure.es.99@aya.yale.edu |
                |  http://www.faradic.net/~mmcclure  |
                --------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Matt McClure <matthew.mcclure.es.99@aya.yale.edu>
Date: 1999/10/13
Raw View
I'm having trouble determining whether the following behavior is
something decreed by the standard or if it is a compiler bug, and I'm
hoping that someone can clarify the standard for me.

In the following example, when A<T>::value_type is used as a type within
a function body (f1(), f1_2()), it is successfully looked up as such.
However, when it is used as a return type or as parameter type, it is
not.

   template <class T> class A {
   public:
      typedef T value_type;
      value_type v;
   };

   template <class T>
   void f1(A<T> a) {
      A<T>::value_type v = a.v;
   }

   template <class T>
   void f1_2() {
      A<T>::value_type v;
   }

   template <class T>
   A<T>::value_type          // error: expected a type specifier
   f2(A<T> a) {
      return a.v;
   }

   template <class T>
   void
   f3(A<T>::value_type v) {  // error: expected a type specifier

   }

   template <class T>
   A<T>::value_type          // error: expected a type specifier
   f4(A<T>::value_type v) {  // error: expected a type specifier
      return v;
   }

Should all uses of A<T>::value_type generate an error?  If not, what
clause in the standard qualifies as the "applicable name lookup"
referred to in 14.6, and which uses above are standard-compliant?

Thanks,
Matt

--
                --------------------------------------
                | matthew.mcclure.es.99@aya.yale.edu |
                |  http://www.faradic.net/~mmcclure  |
                --------------------------------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/10/14
Raw View
Matt McClure wrote:
>
> In the following example, when A<T>::value_type is used as a type within
> a function body (f1(), f1_2()), it is successfully looked up as such.
> However, when it is used as a return type or as parameter type, it is
> not.
>
>    template <class T> class A {
>    public:
>       typedef T value_type;
>       value_type v;
>    };
>
>    template <class T>
>    void f1(A<T> a) {
>       A<T>::value_type v = a.v;
>    }
>
>    template <class T>
>    void f1_2() {
>       A<T>::value_type v;
>    }
>
>    template <class T>
>    A<T>::value_type          // error: expected a type specifier
>    f2(A<T> a) {
>       return a.v;
>    }
>
>    template <class T>
>    void
>    f3(A<T>::value_type v) {  // error: expected a type specifier
>
>    }
>
>    template <class T>
>    A<T>::value_type          // error: expected a type specifier
>    f4(A<T>::value_type v) {  // error: expected a type specifier
>       return v;
>    }
>
> Should all uses of A<T>::value_type generate an error?

Yes. To prevent the error, prefix it with the 'typename' keyword. That
is:

  template < class T >
  typename A<T>::value_type
  f ( typename A<T>::value_type v )
  { return v; }

The reason is, there can exist a specialization of the template class A
where value_type is not a type name. By default, the compiler assumes
that such template members are non-types. To tell the compiler that they
are types, use the 'typename' keyword.

--
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]