Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 07:35 PM
pradosh
Guest
 
Posts: n/a
Default typedef template class

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!

  #2  
Old August 31st, 2005, 07:55 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: typedef template class


"pradosh" <pradosh.sivadoss@gmail.com> wrote in message
news:1125512660.302522.68100@z14g2000cwz.googlegro ups.com...[color=blue]
> Is it possible to typedef a template class? How?[/color]

You must typedef a particular instantiation. See below.
[color=blue]
>
> For example:
>
> template <class T>
> class OldClass
> {
> };
>
> typedef OldClass<T> NewClass<T>;[/color]

typedef OldClass<int> NewClass;
[color=blue]
>
> void main()[/color]

int main()
[color=blue]
> {
> NewClass<int> o;[/color]

NewClass o;
[color=blue]
> }
>
> Thanks![/color]

-Mike


  #3  
Old August 31st, 2005, 08:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: typedef template class

pradosh wrote:[color=blue]
> Is it possible to typedef a template class?[/color]

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".
[color=blue]
> How?
>
> For example:
>
> template <class T>
> class OldClass
> {
> };
>
> typedef OldClass<T> NewClass<T>;
>
> void main()[/color]

int main()
[color=blue]
> {
> NewClass<int> o;
> }[/color]

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
  #4  
Old August 31st, 2005, 08:15 PM
Howard Hinnant
Guest
 
Posts: n/a
Default Re: typedef template class

In article <1125512660.302522.68100@z14g2000cwz.googlegroups. com>,
"pradosh" <pradosh.sivadoss@gmail.com> wrote:
[color=blue]
> 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![/color]

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
  #5  
Old August 31st, 2005, 10:55 PM
Ali Çehreli
Guest
 
Posts: n/a
Default Re: typedef template class

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:J0nRe.31451$Tf5.25546@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> pradosh wrote:[color=green]
>> Is it possible to typedef a template class?[/color]
>
> Technically speaking, there is no such thing as "a template class",[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles