Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Templates within templates

Reply
Thread Tools

Templates within templates

 
 
Tom McCallum
Guest
Posts: n/a
 
      08-04-2004
Is it possible to write something like

template<class T1, class T2>
class CMyClass {
typedef T1<T2> mynewtype; // key line using too of the template
parameters to create a third type
mynewtype x;
}

typedef CMyClass<vector, int> mynewclass;

I have tried but it does not recognise T1 as a templated class even though
the one I am instantiating it with is. The real thing is too big to paste
but the example shows the key line i want to do. I am assuming I need to
say that T1 should be a template but cannot find how to do it, something
like

template<template class T1, class T2>

Thanks in advance for any responses,

Tom


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-04-2004
Tom McCallum wrote:
> Is it possible to write something like
>
> template<class T1, class T2>
> class CMyClass {
> typedef T1<T2> mynewtype; // key line using too of the template


For that you need to declare T1 a template, not a class.

> parameters to create a third type
> mynewtype x;
> }
>
> typedef CMyClass<vector, int> mynewclass;
>
> I have tried but it does not recognise T1 as a templated class even
> though the one I am instantiating it with is. The real thing is too big
> to paste but the example shows the key line i want to do. I am assuming
> I need to say that T1 should be a template but cannot find how to do it,
> something like
>
> template<template class T1, class T2>


That's right. However, in order to use 'std::vector' there, you need
to provide the correct template argument list:

template<template<class T,class = std::allocator<T> > class T1,
class T2>
class CMyClass
{
typedef T1<T2> mynewtype;
};

...

CMyClass<std::vector, int> cmc_v_i;


Victor
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      08-04-2004
On Wed, 04 Aug 2004 15:59:19 GMT, Tom McCallum
<(E-Mail Removed)> wrote:

>Is it possible to write something like
>
>template<class T1, class T2>


You need
template<template<class> class T1, class T2>

>class CMyClass {
> typedef T1<T2> mynewtype; // key line using too of the template
>parameters to create a third type
> mynewtype x;
>}
>
>typedef CMyClass<vector, int> mynewclass;


Ahh, that still won't work since vector takes 2 template parameters
(the second is the allocator). Which means that you need something
like:

template<
template<class, class> class T1,
class T2>
class CMyClass
{
typedef T1<T2, std::allocator<T2> > mynewtype;
mynewtype x;
};

>I have tried but it does not recognise T1 as a templated class even though
>the one I am instantiating it with is. The real thing is too big to paste
>but the example shows the key line i want to do. I am assuming I need to
>say that T1 should be a template but cannot find how to do it, something
>like
>
>template<template class T1, class T2>


Yup, you were close.

Tom
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
2000 Web Templates (Including Customizable Flash Templates) Audrey Pratt C Programming 0 12-14-2005 02:42 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments