typedef template class

P

pradosh

Is it possible to typedef a template class? How?

For example:

template <class T>
class OldClass
{
};

typedef OldClass<T> NewClass<T>;

void main()
{
NewClass<int> o;
}

Thanks!
 
V

Victor Bazarov

pradosh said:
Is it possible to typedef a template class?

Technically speaking, there is no such thing as "a template class", so,
no, there is no way to typedef it. Now, if you mean "a class template",
then the answer is "yes, kinda, but not really".
> How?

For example:

template <class T>
class OldClass
{
};

typedef OldClass<T> NewClass<T>;

void main()

int main()
{
NewClass<int> o;
}

There are no template typedefs yet in the language (there is a proposal
about them, though), so you need to do a small trick to achieve what you
need:

template<class T> class OldClass {};

template<class T> struct NewClass { typedef OldClass<T>::type; };

int main()
{
NewClass<int>::type o; // same as 'OldClass<int> o;'
}

And it probably covered in the FAQ. Please take a read of the FAQ. You
can find it here: http://www.parashift.com/c++-faq-lite/

V
 
H

Howard Hinnant

"pradosh said:
Is it possible to typedef a template class? How?

For example:

template <class T>
class OldClass
{
};

typedef OldClass<T> NewClass<T>;

void main()
{
NewClass<int> o;
}

Thanks!

Sort of, not really. You can do this:

template <class T>
class OldClass
{
};

template <class T>
struct NewClass
{
typedef OldClass<T> type;
};

int main()
{
NewClass<int>::type o;
}

There is a proposal before the committee that would allow the following:

template <class T>
class OldClass
{
};

template <class T>
using NewClass = OldClass<T>;

int main()
{
NewClass<int> o;
}

To the best of my knowledge this one has general support on the
committee and the authors are working on proposed wording.

-Howard
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Victor Bazarov said:
Technically speaking, there is no such thing as "a template class",

It is obvious that the original poster meant "class template"; but
confusingly, the term "template class" exists as well.

It's sometimes used in place of "class template," but sometimes used to mean
an instance of a class template. David Vandevoorde and Nicolai Josuttis
accept this usage, explain the situation in Chapter 7 of their book C++
Templates, and avoid to use the term :)

If we accept "template class" to mean an instance of a class, then the
answer to the original question is trivial:

typedef OldClass<int> NewClass;

But OP didn't ask that :)

Ali
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top