By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Templates within templates

P: n/a
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/
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 22 '05 #2

P: n/a
On Wed, 04 Aug 2004 15:59:19 GMT, Tom McCallum
<te********@hotmail.com> 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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.