Templates within templates

Discussion in 'C++' started by Tom McCallum, Aug 4, 2004.

  1. Tom McCallum

    Tom McCallum Guest

    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/
    Tom McCallum, Aug 4, 2004
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 4, 2004
    #2
    1. Advertising

  3. Tom McCallum

    tom_usenet Guest

    On Wed, 04 Aug 2004 15:59:19 GMT, Tom McCallum
    <> 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
    tom_usenet, Aug 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    1
    Views:
    597
    Neredbojias
    Sep 26, 2005
  2. John Harrison

    using templates in templates

    John Harrison, Jul 31, 2003, in forum: C++
    Replies:
    8
    Views:
    380
    Torsten Curdt
    Jul 31, 2003
  3. JKop
    Replies:
    3
    Views:
    468
  4. Audrey Pratt
    Replies:
    0
    Views:
    3,269
    Audrey Pratt
    Dec 14, 2005
  5. recover
    Replies:
    2
    Views:
    801
    recover
    Jul 25, 2006
Loading...

Share This Page