Template specialization for vectors of any type

Discussion in 'C++' started by mrstephengross, Nov 7, 2006.

  1. Ok, I've got a templated class Foo:

    template<typename T> class Foo { ; };

    I want to specialize Foo for vectors holding any type:

    // Something like:
    template<typename C> class Foo<std::vector<C> > { /* ... */ };

    Is there any way to do this? Template template parameters, maybe?

    Thanks!
    --Steve ()
     
    mrstephengross, Nov 7, 2006
    #1
    1. Advertising

  2. mrstephengross wrote:
    > Ok, I've got a templated class Foo:
    >
    > template<typename T> class Foo { ; };


    What does the semicolon between the braces do?

    > I want to specialize Foo for vectors holding any type:
    >
    > // Something like:
    > template<typename C> class Foo<std::vector<C> > { /* ... */ };
    >
    > Is there any way to do this? Template template parameters, maybe?


    But you just did it! Doesn't it work for you? See FAQ 5.8.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 7, 2006
    #2
    1. Advertising

  3. mrstephengross

    Kai-Uwe Bux Guest

    mrstephengross wrote:

    > Ok, I've got a templated class Foo:
    >
    > template<typename T> class Foo { ; };
    >
    > I want to specialize Foo for vectors holding any type:
    >
    > // Something like:
    > template<typename C> class Foo<std::vector<C> > { /* ... */ };


    Have you tried? Looks good to me. You may want to take care of the allocator
    template parameter, too:

    template < typename Val, typename Alloc >
    class Foo< std::vector<Val,Alloc> > { /* ... */ };

    > Is there any way to do this? Template template parameters, maybe?


    No need for that.



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 7, 2006
    #3
  4. mrstephengross

    terminator Guest

    Kai-Uwe Bux wrote:
    > mrstephengross wrote:
    >
    > > Ok, I've got a templated class Foo:
    > >
    > > template<typename T> class Foo { ; };
    > >
    > > I want to specialize Foo for vectors holding any type:
    > >
    > > // Something like:
    > > template<typename C> class Foo<std::vector<C> > { /* ... */ };

    >
    > Have you tried? Looks good to me. You may want to take care of the allocator
    > template parameter, too:
    >
    > template < typename Val, typename Alloc >
    > class Foo< std::vector<Val,Alloc> > { /* ... */ };
    >
    > > Is there any way to do this? Template template parameters, maybe?

    >
    > No need for that.
    >
    >
    >
    > Best
    >
    > Kai-Uwe Bux

    the question looks to be like this:
    //first define a template
    template <class T> class tmplt {/*etc*/};
    template <class C> class othertmplt;
    //now specialize with templated parameter
    template <class C> class tmplt < othertmplt<C> >{/*etc*/};

    does such code compile? isn`t there any restrictions about
    specialization?
     
    terminator, Nov 7, 2006
    #4
    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.

Share This Page