How to get template parameter, when i match a template?

Discussion in 'C++' started by abir, Jun 30, 2008.

  1. abir

    abir Guest

    I am matching a template, and specializing based of a template, rather
    than a single class.

    The codes are like,
    template<template<typename T,typename Alloc = std::allocator<T>
    >class C>

    class pix{
    };

    template<>
    class pix<vector>{
    };
    The matching works perfectly.
    Now how will i get the template parameters in the specialized class?
    I want to do things like,
    template<>
    class pix<std::vector>{
    private:
    std::vector<T>* v_;
    };
    From where will I get T & Alloc fro which it is a match?

    Thanks
    abir
     
    abir, Jun 30, 2008
    #1
    1. Advertising

  2. abir

    Kai-Uwe Bux Guest

    abir wrote:

    > I am matching a template, and specializing based of a template, rather
    > than a single class.
    >
    > The codes are like,
    > template<template<typename T,typename Alloc = std::allocator<T>
    >>class C>

    > class pix{
    > };
    >
    > template<>
    > class pix<vector>{
    > };
    > The matching works perfectly.
    > Now how will i get the template parameters in the specialized class?
    > I want to do things like,
    > template<>
    > class pix<std::vector>{
    > private:
    > std::vector<T>* v_;
    > };


    Why a pointer to a vector. That looks fishy.

    > From where will I get T & Alloc fro which it is a match?


    You won't get them from anywhere. You have to supply them.

    BTW: this is not related to the class being specialized. If you consider the
    unspecialized pix, you will find that you cannot get at T and Alloc either
    because they are just placeholders for something you still need to specify.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 30, 2008
    #2
    1. Advertising

  3. abir

    abir Guest

    On Jun 30, 4:52 pm, Kai-Uwe Bux <> wrote:
    > abir wrote:
    > > I am matching a template, and specializing based of a template, rather
    > > than a single class.

    >
    > > The codes are like,
    > > template<template<typename T,typename Alloc = std::allocator<T>
    > >>class C>

    > > class pix{
    > > };

    >
    > > template<>
    > > class pix<vector>{
    > > };
    > > The matching works perfectly.
    > > Now how will i get the template parameters in the specialized class?
    > > I want to do things like,
    > > template<>
    > > class pix<std::vector>{
    > > private:
    > > std::vector<T>* v_;
    > > };

    >
    > Why a pointer to a vector. That looks fishy.
    >
    > > From where will I get T & Alloc fro which it is a match?

    >
    > You won't get them from anywhere. You have to supply them.
    >
    > BTW: this is not related to the class being specialized. If you consider the
    > unspecialized pix, you will find that you cannot get at T and Alloc either
    > because they are just placeholders for something you still need to specify.
    >
    > Best
    >
    > Kai-Uwe Bux


    It can be reference to a vector instead of pointer. I want to have an
    external iterator kind of thing, which i name pix, which is portable,
    i.e can be stored as long as the element at the location exists,
    irrespective of whether container resizes or not. (Of course they can
    be made immune to pop_front,push_front, insert, erase etc also)
    The pointer to a vector is not at all fishy in this case, almost all
    (not all) iterators has a pointer to the container.
    I want something like,
    typedef vector<int> V;
    V v; v.push_back() a few elements ...
    pix<V> p(v);
    ++p; *p; get element
    v.push_back() a few more.
    *p to get element.

    so for a container C , i wanted pix as,
    template<typename C>
    class pix{
    C& c_;
    C::size_type index_;
    };
    when C is a vector, but another implementation for another C. Only
    problem is C itself is a template.

    Why the template template parameters are "just placeholders" when the
    instance is a full class?
    Any other way to do it?

    Thanks
    abir
     
    abir, Jun 30, 2008
    #3
  4. abir

    Kai-Uwe Bux Guest

    abir wrote:

    > On Jun 30, 4:52 pm, Kai-Uwe Bux <> wrote:
    >> abir wrote:
    >> > I am matching a template, and specializing based of a template, rather
    >> > than a single class.

    >>
    >> > The codes are like,
    >> > template<template<typename T,typename Alloc = std::allocator<T>
    >> >>class C>
    >> > class pix{
    >> > };

    >>
    >> > template<>
    >> > class pix<vector>{
    >> > };
    >> > The matching works perfectly.
    >> > Now how will i get the template parameters in the specialized class?
    >> > I want to do things like,
    >> > template<>
    >> > class pix<std::vector>{
    >> > private:
    >> > std::vector<T>* v_;
    >> > };

    >>
    >> Why a pointer to a vector. That looks fishy.
    >>
    >> > From where will I get T & Alloc fro which it is a match?

    >>
    >> You won't get them from anywhere. You have to supply them.
    >>
    >> BTW: this is not related to the class being specialized. If you consider
    >> the unspecialized pix, you will find that you cannot get at T and Alloc
    >> either because they are just placeholders for something you still need to
    >> specify.
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > It can be reference to a vector instead of pointer. I want to have an
    > external iterator kind of thing, which i name pix, which is portable,
    > i.e can be stored as long as the element at the location exists,
    > irrespective of whether container resizes or not. (Of course they can
    > be made immune to pop_front,push_front, insert, erase etc also)
    > The pointer to a vector is not at all fishy in this case, almost all
    > (not all) iterators has a pointer to the container.
    > I want something like,
    > typedef vector<int> V;
    > V v; v.push_back() a few elements ...
    > pix<V> p(v);
    > ++p; *p; get element
    > v.push_back() a few more.
    > *p to get element.
    >
    > so for a container C , i wanted pix as,
    > template<typename C>
    > class pix{
    > C& c_;
    > C::size_type index_;
    > };
    > when C is a vector, but another implementation for another C. Only
    > problem is C itself is a template.


    I see. In this case, I think, you want a pointer and not a reference. That
    way, you won't run into trouble implementing an assignment operator for
    pix.


    > Why the template template parameters are "just placeholders" when the
    > instance is a full class?


    This is too vague. The instance of what is a class?

    Anyway, when you have a template template parameter, what you pass as a
    parameter is a template. In you case, you pass std::vector. Note that you
    do not pass an instantiation of that template, i.e., you do not pass
    something like std::vector<int>. You just pass std::vector. The pix class
    would be able to use the parameter C to do stuff like C<int>.

    > Any other way to do it?


    You don't want to use template template parameters. Use an ordinary typename
    parameter:

    template < typename Sequence >
    class pix;

    and then specialize for vectors:

    template < typename T, typename A >
    class pix< std::vector< T, A > > {
    ...
    };

    for deques:

    template < typename T, typename A >
    class pix< std::deque< T, A > > {
    ...
    };

    and for lists:

    template < typename T, typename A >
    class pix< std::list< T, A > > {
    ...
    };

    (not run by a compiler)


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 30, 2008
    #4
  5. abir

    abir Guest

    On Jun 30, 5:31 pm, Kai-Uwe Bux <> wrote:
    > abir wrote:
    > > On Jun 30, 4:52 pm, Kai-Uwe Bux <> wrote:
    > >> abir wrote:
    > >> > I am matching a template, and specializing based of a template, rather
    > >> > than a single class.

    >
    > >> > The codes are like,
    > >> > template<template<typename T,typename Alloc = std::allocator<T>
    > >> >>class C>
    > >> > class pix{
    > >> > };

    >
    > >> > template<>
    > >> > class pix<vector>{
    > >> > };
    > >> > The matching works perfectly.
    > >> > Now how will i get the template parameters in the specialized class?
    > >> > I want to do things like,
    > >> > template<>
    > >> > class pix<std::vector>{
    > >> > private:
    > >> > std::vector<T>* v_;
    > >> > };

    >
    > >> Why a pointer to a vector. That looks fishy.

    >
    > >> > From where will I get T & Alloc fro which it is a match?

    >
    > >> You won't get them from anywhere. You have to supply them.

    >
    > >> BTW: this is not related to the class being specialized. If you consider
    > >> the unspecialized pix, you will find that you cannot get at T and Alloc
    > >> either because they are just placeholders for something you still need to
    > >> specify.

    >
    > >> Best

    >
    > >> Kai-Uwe Bux

    >
    > > It can be reference to a vector instead of pointer. I want to have an
    > > external iterator kind of thing, which i name pix, which is portable,
    > > i.e can be stored as long as the element at the location exists,
    > > irrespective of whether container resizes or not. (Of course they can
    > > be made immune to pop_front,push_front, insert, erase etc also)
    > > The pointer to a vector is not at all fishy in this case, almost all
    > > (not all) iterators has a pointer to the container.
    > > I want something like,
    > > typedef vector<int> V;
    > > V v; v.push_back() a few elements ...
    > > pix<V> p(v);
    > > ++p; *p; get element
    > > v.push_back() a few more.
    > > *p to get element.

    >
    > > so for a container C , i wanted pix as,
    > > template<typename C>
    > > class pix{
    > > C& c_;
    > > C::size_type index_;
    > > };
    > > when C is a vector, but another implementation for another C. Only
    > > problem is C itself is a template.

    >
    > I see. In this case, I think, you want a pointer and not a reference. That
    > way, you won't run into trouble implementing an assignment operator for
    > pix.
    >

    Yes, that is the only reason I had to use pointer instead of
    reference.
    > > Why the template template parameters are "just placeholders" when the
    > > instance is a full class?

    >
    > This is too vague. The instance of what is a class?
    >
    > Anyway, when you have a template template parameter, what you pass as a
    > parameter is a template. In you case, you pass std::vector. Note that you
    > do not pass an instantiation of that template, i.e., you do not pass
    > something like std::vector<int>. You just pass std::vector. The pix class
    > would be able to use the parameter C to do stuff like C<int>.
    >
    > > Any other way to do it?

    >
    > You don't want to use template template parameters. Use an ordinary typename
    > parameter:
    >
    > template < typename Sequence >
    > class pix;
    >
    > and then specialize for vectors:
    >
    > template < typename T, typename A >
    > class pix< std::vector< T, A > > {
    > ...
    > };
    >
    > for deques:
    >
    > template < typename T, typename A >
    > class pix< std::deque< T, A > > {
    > ...
    > };
    >
    > and for lists:
    >
    > template < typename T, typename A >
    > class pix< std::list< T, A > > {
    > ...
    > };
    >
    > (not run by a compiler)
    >

    Got it. It runs perfectly & works according to my wishes!
    Thanks a lot.
    BTW, is there any already existing code which can refer to a slice of
    sequence which doesn't get invalidated due to memory capacity related
    issues? (I know blitz array boost numerics etc have slicing , but the
    containers are non re sizable there ). I am very much in need for
    that.

    > Best
    >
    > Kai-Uwe Bux

    abir
     
    abir, Jun 30, 2008
    #5
    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. hiwa
    Replies:
    0
    Views:
    639
  2. Old Wolf
    Replies:
    0
    Views:
    553
    Old Wolf
    Mar 14, 2005
  3. Stuart Redmann
    Replies:
    5
    Views:
    481
    Stuart Redmann
    Dec 14, 2007
  4. Isti
    Replies:
    2
    Views:
    724
  5. Abdul Mohsin
    Replies:
    1
    Views:
    266
Loading...

Share This Page