Templates within templates

T

Tom McCallum

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
 
V

Victor Bazarov

Tom said:
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
 
T

tom_usenet

Is it possible to write something like

template<class T1, class T2>

You need
template said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Class templates and singleton container 3
templates?? 2
Templates and g++ 4
templates help 1
templates 10
gdb breakpoint issue with C++ templates 8
Templates 5
specialaizing templates --error 3

Members online

Forum statistics

Threads
473,765
Messages
2,569,568
Members
45,042
Latest member
icassiem

Latest Threads

Top