|
|
 |
 |
 |
 |
typename vs class
Hello, I've seen two forms of template declarations template<typename T> class A{...}; and template<class T> class B{...}; what's the difference between "typename" and "class"? I think both declare "T" as a type variable. Thanks, Jess
Jess wrote: > Hello, > I've seen two forms of template declarations > template<typename T> > class A{...}; > and > template<class T> > class B{...}; > what's the difference between "typename" and "class"? I think both > declare "T" as a type variable.
No difference at all ;) Regards, Zeppe
On Jun 6, 6:14 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.it> wrote: > Jess wrote: > > Hello, > > I've seen two forms of template declarations > > template<typename T> > > class A{...}; > > and > > template<class T> > > class B{...}; > > what's the difference between "typename" and "class"? I think both > > declare "T" as a type variable. > No difference at all ;) > Regards, > Zeppe- Hide quoted text - >
there is a difference when u do a typedef class vs typdef typename i dont recall the exact diff
Jess wrote: > Hello, > I've seen two forms of template declarations > template<typename T> > class A{...}; > and > template<class T> > class B{...}; > what's the difference between "typename" and "class"?
In this situation: "Class" is shorter and faster to type, while many find "typename" to be more expressive about the usage and maybe more newbie-friendly. Originally, only "class" was a keyword in pre-standard C++, so "class" was adopted to templates when they were introduced. At some time, the language lawyers saw that "typename" was needed (for other reasons), and when first introduced, it was also found suitable in this situation. -- rbh
Jess wrote:
:: Hello, :: :: I've seen two forms of template declarations :: :: template<typename T> :: class A{...}; :: :: and :: :: template<class T> :: class B{...}; :: :: what's the difference between "typename" and "class"? I think both :: declare "T" as a type variable. :: Used this way, there is no difference. In other places, typename and class have distinct uses. Bo Persson
arun.da @gmail.com wrote: > On Jun 6, 6:14 pm, Zeppe > <zep_p@.remove.all.this.long.comment.yahoo.it> wrote: >> Jess wrote: >>> Hello, >>> I've seen two forms of template declarations >>> template<typename T> >>> class A{...}; >>> and >>> template<class T> >>> class B{...}; >>> what's the difference between "typename" and "class"? I think both >>> declare "T" as a type variable. >> No difference at all ;) > there is a difference when u do a typedef class vs typedef typename > i dont recall the exact diff
The keyword "typename" is needed to tell the compiler that the following is a type, and not something else, and it is used when the compiler cannot possibly know. Example: template <class T> // or <typename T> void f() { T::x(y); }
At this point, the exact type of T is unknown, and there is no possible way of saying what x is. Is x a static member of some class, or is it a type? In this situation, the rules of C++ says that T::x is not a type, so if it is, then you have to say so with typename: template <class T> // or <typename T> void f() { typename T::x(y); //T::x is a type }
This also effects what y is. In the first example, y must be some global variable, but in the latter, y is declared as a T::x (in a really horrible manner). -- rbh
|
 |
 |
 |
 |
|