Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typedef with template classes

7 views
Skip to first unread message

Alex P.

unread,
Apr 1, 2001, 4:46:59 PM4/1/01
to
It is well known that typedef can be used for aliasing type names, as in

typedef unsigned long ulong;
typedef MyClassWithAVeryLongName MCWAVLN;

How do you do this for a template class? I've tried the following with no
luck (MSVC++ 6.0 SP4):

typedef MyTemplateClass MTC; // doesn't work
typedef MyTemplateClass<T> MTC<T>; // doesn't work since it doesn't know
about T
typedef template< typename T >
MyTemplateClass<T>
template< typename T >
MTC<T>; // doesn't work

Can it even be done? Any ideas?

TIA
Alex


Victor Bazarov

unread,
Apr 1, 2001, 5:00:55 PM4/1/01
to
"Alex P." <robo...@hotmail.com> wrote...

You can use 'typedef' to define an alias to a real type, not
to a templated one. With a template you can use 'typedef' to
define an alias to a template specialisation (but not to a
partial one).

You could try using #define to shorten the name.

#define MTC(T) MyTemplateClass<T>

Of course, you lose the angle brackets and are forced to use
parentheses.

Victor
--
Please remove capital A's from my address when replying by mail


Pete Becker

unread,
Apr 1, 2001, 5:37:57 PM4/1/01
to

A template is not a type.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Dennis Yelle

unread,
Apr 1, 2001, 6:28:38 PM4/1/01
to

Well, you can do this:

// Just to get a shorter name.
template< class T> class MTC : public MyTemplateClass<T> {};

or this

#define MTC MyTemplateClass

Dennis Yelle
--
I am a computer programmer and I am looking for a job.
There is a link to my resume here:
http://table.jps.net/~vert/

Mike Wahler

unread,
Apr 1, 2001, 10:57:12 PM4/1/01
to

Alex P. wrote in message
<7tMx6.176244$tP3.2...@news1.rdc1.bc.home.com>...

A typdef must alias a complete type, which a template is not.
The best you can do is typedef particular instantiations:

typedef MyTemplate<int> MTI;

The only way to (sort of) do what you're asking
is with a macro, as others have shown.

-Mike

Alex P.

unread,
Apr 1, 2001, 11:11:56 PM4/1/01
to

"Dennis Yelle" <denn...@jps.net> wrote in message
news:3AC7AB96...@jps.net...

>
> // Just to get a shorter name.
> template< class T> class MTC : public MyTemplateClass<T> {};

Actually, this is not entirely correct. MTC will never be able to access
MyTemplateClass's private members this way! The remedy would be to make MTC
a friend of MyTemplateClass, so we're talking about "friend templates",
which the standard requires, but unfortunately, is not supported by all
compilers including mine (MSVC++ 6.0 SP4), as far as I know.

Alex

R. Sinoradzki

unread,
Apr 2, 2001, 9:26:43 AM4/2/01
to
"Alex P." wrote:
>
> "Dennis Yelle" <denn...@jps.net> wrote in message
> news:3AC7AB96...@jps.net...
> >
> > // Just to get a shorter name.
> > template< class T> class MTC : public MyTemplateClass<T> {};
>
> Actually, this is not entirely correct. MTC will never be able to access
> MyTemplateClass's private members this way! The remedy would be to make MTC
> a friend of MyTemplateClass, so we're talking about "friend templates",
> which the standard requires, but unfortunately, is not supported by all
> compilers including mine (MSVC++ 6.0 SP4), as far as I know.
>
> Alex

Looks like the last GOTW ...
I see another problem. If you use this to shorten names, and you
write a functions like:

template <class T>
void foo( const MTC<T>& m){
//....
}

you cannot use it with the long name anymore because you need a dynamic cast
for this. (Perhaps I understood something wrong ... still learning ....)

bye Ralf

Will Waterson

unread,
Apr 25, 2001, 7:26:37 PM4/25/01
to
You can actually workaround this "template typedef" problem using a
technique that you find at:

"Gnarly New C++ Language Features (that you can finally use)"
http://www.cantrip.org/gnarly.html

Its not as pretty as the straight typedef that you describe.... its
mainly to do with member typedefs of template classes + the "typename"
keyword. The above link should explain it all.

Cheers

Will

willw.vcf
0 new messages