Template specialization help

Discussion in 'C++' started by Adrian, Feb 19, 2011.

  1. Adrian

    Adrian Guest

    I am trying to write a specialized version of print that will
    automagically work out that the container is a pointer of containers and
    does a double ref on each iterator.

    It feels possible but I cannot think of a way to do it. My initial
    thoughts where to use the STL contains ::value_type and ::const_pointer
    typedefs

    Happy to modify template params to pass enough information

    Anyone done this, or got any ideas on how

    Thanks

    TIA

    Adrian


    #include <iostream>
    #include <vector>

    template<class T>
    void print(T first, T last)
    {
    for(T i=first; i!=last; ++i)
    std::cout << (*i) << std::endl;
    }

    #if 0
    template<>
    void print(T first, T last)
    {
    for(T i=first; i!=last; ++i)
    std::cout << (**i) << std::endl;
    }
    #endif
    int main(int argc, char *argv[])
    {
    std::vector<int> intlist;
    std::vector<int*> intptrlist;

    for(int i=0; i<10; ++i)
    {
    intlist.push_back(i);
    intptrlist.push_back(new int(i));
    }

    print(intlist.begin(), intlist.end());
    print(intptrlist.begin(), intptrlist.end());

    return 0;
    }
    Adrian, Feb 19, 2011
    #1
    1. Advertising

  2. Adrian

    Adrian Guest

    On 2/18/2011 5:50 PM, Adrian wrote:
    > container is a pointer of containers

    Sorry. Should be
    container is a container of pointers
    Adrian, Feb 19, 2011
    #2
    1. Advertising

  3. Adrian

    Ian Collins Guest

    On 02/19/11 01:50 PM, Adrian wrote:
    > I am trying to write a specialized version of print that will
    > automagically work out that the container is a pointer of containers and
    > does a double ref on each iterator.
    >
    > It feels possible but I cannot think of a way to do it. My initial
    > thoughts where to use the STL contains ::value_type and ::const_pointer
    > typedefs
    >
    > Happy to modify template params to pass enough information
    >
    > Anyone done this, or got any ideas on how


    Um, I'm sure there's a cleaner way, but these work for ordered and
    unordered containers:

    template <typename T,
    template <typename E,
    typename A = std::allocator<E> > class Container>
    void print( const Container<T>& c )
    {
    for( typename Container<T>::const_iterator i = c.begin(); i !=
    c.end(); ++i )
    std::cout << *i << std::endl;
    }

    template <typename T,
    template <typename E,
    typename L = std::less<E>,
    typename A = std::allocator<E> > class Container>
    void print( const Container<T>& c )
    {
    for( typename Container<T>::const_iterator i = c.begin(); i !=
    c.end(); ++i )
    std::cout << *i << std::endl;
    }

    template <typename T,
    template <typename E,
    typename A = std::allocator<E> > class Container>
    void print( const Container<T*>& c )
    {
    for( typename Container<T*>::const_iterator i = c.begin(); i !=
    c.end(); ++i )
    std::cout << **i << std::endl;
    }

    template <typename T,
    template <typename E,
    typename L = std::less<E>,
    typename A = std::allocator<E> > class Container>
    void print( const Container<T*>& c )
    {
    for( typename Container<T*>::const_iterator i = c.begin(); i !=
    c.end(); ++i )
    std::cout << **i << std::endl;
    }

    The horrible template template syntax will be much cleaner in C++0x.

    > int main(int argc, char *argv[])
    > {
    > std::vector<int> intlist;
    > std::vector<int*> intptrlist;
    >
    > for(int i=0; i<10; ++i)
    > {
    > intlist.push_back(i);
    > intptrlist.push_back(new int(i));
    > }
    >
    > print(intlist.begin(), intlist.end());
    > print(intptrlist.begin(), intptrlist.end());
    >
    > return 0;
    > }


    --
    Ian Collins
    Ian Collins, Feb 19, 2011
    #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. David B. Held
    Replies:
    2
    Views:
    459
    Rob Williscroft
    Oct 26, 2003
  2. Dave
    Replies:
    4
    Views:
    7,642
    pdixtl
    Jun 4, 2010
  3. pit3k
    Replies:
    8
    Views:
    360
    Victor Bazarov
    Feb 9, 2005
  4. case2005
    Replies:
    3
    Views:
    1,788
    Nicolas Pavlidis
    Feb 13, 2005
  5. Joseph Turian
    Replies:
    2
    Views:
    454
Loading...

Share This Page