Topic: Difference between typename and class


Author: James Kuyper <kuyper@wizard.net>
Date: 1998/02/24
Raw View
Oleg Zabluda wrote:
>
> Steve Clamage <clamage@Eng.sun.com> wrote:
...
> : The typename keyword was added to enable better (earlier) error detection
> : in templates. When you mention a member of a type parameter to a template,
> : the member is assumed not to be a type unless you use the keyword
> : typename:
> :       template < class T > void f(const T& t) {
> :               T::A * a;
> :               typename T::B b;
> :       }
> : The first line of the function is not a variable declaration, but
> : the second line is. The first line might be valid if an object "a"
> : is visible and a suitable operator* is potentially available.
>
> Could you give me an example of a class T such that the first line is
> valid in any way, but being a declaration of a variable named ``a'' with
> the type T::A* ?
>

class Example
{
 public:
  static int A;
  typedef double B;
};

int a;

With those two declarations in scope when 'f' is defined, f<Example>(t)
would calculate and discard the product of Example::A and a, and create
and discard an double named 'b'. With A and B interchanged in either
place (but not both), it would trigger error messages.


[ 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: James Kuyper <kuyper@wizard.net>
Date: 1998/02/25
Raw View
Alexandre Oliva wrote:
>=20
> David Vandevoorde writes:
>=20
> >> ----------------------
> >> template<class T> struct A {
> >> static T t;
> >> };
> >> typedef int function();
> >> A<function> a; // ill=ADformed: would declare A<function>::t
> >> // as a static member function
> >> ------------
> >> I understand that, if not for the keyword 'static', this
> >> would have been valid C++. Right?
>=20
> > Yes.
>=20
> How come?  Being function a non-member function type, what would
> A<function>::t be?

Without the 'static',=20

 A<function> a;

would be roughly equivalent to=20

struct A_function{
 int t();
} a;


[ 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: Daveed Vandevoorde <daveed@cup.hp.com>
Date: 1998/02/20
Raw View
David Vandevoorde wrote:
[...]
> > ----------------------
> > template<class T> struct A {
> > static T t;
> > };
> > typedef int function();
> > A<function> a; // ill=ADformed: would declare A<function>::t
> > // as a static member function
> > ------------
> > I understand that, if not for the keyword 'static', this
> > would have been valid C++. Right?
>=20
> Yes.

Oops---I retract my answer. Even without `static' this is
illegal (though it would be legal if T were a non-dependent
typedef instead of a template parameter).

 Daveed


[ 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: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/02/21
Raw View
David Vandevoorde writes:

>> ----------------------
>> template<class T> struct A {
>> static T t;
>> };
>> typedef int function();
>> A<function> a; // ill=ADformed: would declare A<function>::t
>> // as a static member function
>> ------------
>> I understand that, if not for the keyword 'static', this
>> would have been valid C++. Right?

> Yes.

How come?  Being function a non-member function type, what would
A<function>::t be?

--=20
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ 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: James Kuyper <kuyper@wizard.net>
Date: 1998/02/21
Raw View
Piet Van Vlierberghe wrote:
>
> Throughout the december 1996 draft, class and typename interchangeable when
> used
> in (formal) template parameters. Is there no difference? If not, why
> introduce the redundancy?

I don't know, but I wouldn't be surprised to learn that it was for
backward compatibility with two different template traditions.

I use 'typename' when the intended range of applications includes the
standard types, such as 'int'. I use 'class' when the template could
only be meaningfully applied to a class. However, that is purely for the
human reader.
---
[ 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: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/02/21
Raw View
Steve Clamage <clamage@Eng.sun.com> wrote:
: In article 7f0acac1@amundsen, "Piet Van Vlierberghe" <pvv@lms.be> writes:
: >Throughout the december 1996 draft, class and typename interchangeable when
: >used
: >in (formal) template parameters. Is there no difference? If not, why
: >introduce the redundancy?

: The typename keyword was added to enable better (earlier) error detection
: in templates. When you mention a member of a type parameter to a template,
: the member is assumed not to be a type unless you use the keyword
: typename:
:  template < class T > void f(const T& t) {
:   T::A * a;
:   typename T::B b;
:  }
: The first line of the function is not a variable declaration, but
: the second line is. The first line might be valid if an object "a"
: is visible and a suitable operator* is potentially available.

Could you give me an example of a class T such that the first line is
valid in any way, but being a declaration of a variable named ``a'' with
the type T::A* ?

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ 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: "Piet Van Vlierberghe" <pvv@lms.be>
Date: 1998/02/18
Raw View
Throughout the december 1996 draft, class and typename interchangeable when
used
in (formal) template parameters. Is there no difference? If not, why
introduce the redundancy?

The following example was taken from the draft. Here, a function pointer is
passed as a class.
This surely looks sloppy to me:
----------------------
template<class T> struct A {
static T t;
};
typedef int function();
A<function> a; // ill   formed: would declare A<function>::t
// as a static member function
------------
I understand that, if not for the keyword 'static', this would have been
valid C++. Right?
---
[ 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: David Vandevoorde <daveed@cup.hp.com>
Date: 1998/02/19
Raw View
Piet Van Vlierberghe wrote:
>=20
> Throughout the december 1996 draft, class and typename
> interchangeable when used in (formal) template parameters.
> Is there no difference? If not, why introduce the redundancy?

The keyword typename was primarily introduced to indicate
that qualified names with dependent qualifiers refer to
typenames.

The use of `class' to indicate type-parameters seemed to
suggest that only class-type template arguments were allowed
for those parameters. Since the new keyword was needed
anyway, it could be ``overloaded'' for type-parameters as
well. In that context it has _exactly_ the same semantics
as the keyword `class'.

> The following example was taken from the draft. Here, a
> function pointer is passed as a class.

Not as a `class' but as a general type-argument. The use
of `typename' would not lead to the confusion, but `class'
had been there for several years--it couldn't just been
removed.

> This surely looks sloppy to me:

The WP is not a tutorial though. Examples tend to clarify
subtle points. They're typically not suited for gaining
initial familiarity with the language.

> ----------------------
> template<class T> struct A {
> static T t;
> };
> typedef int function();
> A<function> a; // ill=ADformed: would declare A<function>::t
> // as a static member function
> ------------
> I understand that, if not for the keyword 'static', this
> would have been valid C++. Right?

Yes.

 Daveed
---
[ 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: clamage@Eng.sun.com (Steve Clamage)
Date: 1998/02/19
Raw View
In article 7f0acac1@amundsen, "Piet Van Vlierberghe" <pvv@lms.be> writes:
>Throughout the december 1996 draft, class and typename interchangeable when
>used
>in (formal) template parameters. Is there no difference? If not, why
>introduce the redundancy?

The typename keyword was added to enable better (earlier) error detection
in templates. When you mention a member of a type parameter to a template,
the member is assumed not to be a type unless you use the keyword
typename:
 template < class T > void f(const T& t) {
  T::A * a;
  typename T::B b;
 }
The first line of the function is not a variable declaration, but
the second line is. The first line might be valid if an object "a"
is visible and a suitable operator* is potentially available.

Since the keyword "class" is somewhat misleading (the type might
not be a class, but might be a simple type), the use of typename
was extended to specify that a template parameter is a type:
 template < typename T > void f( ...

Since "class" had already been in the language for many years for
that purpose, it wasn't reasonable to disallow it. Thus, you have
two ways to say the same thing.

In that specific context, the two keywords are exactly equivalent.
I don't think there is any other context where you could use either
keyword.

---
Steve Clamage, stephen.clamage@sun.com



[ 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              ]