473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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!

Aug 31 '05 #1
4 14016

"pradosh" <pr**************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Is it possible to typedef a template class? How?
You must typedef a particular instantiation. See below.

For example:

template <class T>
class OldClass
{
};

typedef OldClass<T> NewClass<T>;
typedef OldClass<int> NewClass;

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

Thanks!


-Mike
Aug 31 '05 #2
pradosh wrote:
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
Aug 31 '05 #3
In article <11*********************@z14g2000cwz.googlegroups. com>,
"pradosh" <pr**************@gmail.com> wrote:
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
Aug 31 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:J0*******************@newsread1.mlpsca01.us.t o.verio.net...
pradosh wrote:
Is it possible to typedef a template class?


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

Aug 31 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Arkadiy Vertleyb | last post by:
Hi all, I am having a problem trying to overload a function template, based on a typedef, such as: template<class T> struct A {}; template<class T>
5
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
5
by: Alan Woodland | last post by:
Hi, I don't think I can't do this directly with standard C++, I've tried all the ways I could think of that make sense and then some. I was wondering if someone had a genius idea how I could...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
3
by: Adam Nielsen | last post by:
Hi everyone, Yet another syntax problem that's baffling me with templates. I want to instantiate a template with a single parameter as per normal, however the parameter is actually a template...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.