typename vs class

J

Jess

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
 
Z

Zeppe

Jess said:
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
 
A

arun.darra

No difference at all ;)

Regards,

Zeppe- Hide quoted text -

- Show quoted text -

there is a difference when u do a typedef class vs typdef typename
i dont recall the exact diff
 
R

Robert Bauck Hamar

Jess said:
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.
 
B

Bo Persson

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
 
R

Robert Bauck Hamar

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).
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top