locked
template template parameter RRS feed

  • Question

  • Hi,

    I was trying write a sample program for a class template that accept class template , i.e template template parameter.

    But while compiling it in Vs2008 compiler was crashing, Please check below code.

    #include<iostream>

    #include <vector>

    using namespace std;

     template<class T, template <class T2,  class = allocator<T2> > class STL,int n=5>

    class Col

    {

    private:

       T data[n];

       STL<T> stl;

    public:

       void fill()   {

          for(int i=0;i<n; ++i)      {

            stl.push_back(i);

          }

       }

       class iterator;

       friend class iterator;

       class iterator   {

       private:

          int index;

          Col<T, n> cc;

       public:

          iterator(Col<T, n> c,int i):index(i), cc(c) {}

          iterator(Col<T, n> c):index(n), cc(c) {}

          T operator*() { return cc.data[index]; }

          //T operator++(int x) { return index++;}

          iterator operator++(int x) { return iterator(cc,index++);}

          bool operator!=(const iterator& rhs ) { return index < rhs.index;}

       };

       iterator begin()   {

          //return iterator(*this, 0);

          return stl.begin();

       }

       iterator end()   {

          //return iterator(*this);

          return stl.end();

       }

    };

     int main(){

       Col<int, vector> c;

       c.fill();

       Col<int,5>::iterator it = c.begin();

        while( it != c.end() )   {

          cout << *it;

          it++;

       }

       return 0;

    };

    May I know what is wrong in this code ?


    Thanks, Jafar


    Thursday, November 13, 2014 6:30 PM

Answers

  • You may need to elaborate on what you are trying to solve with this template in a template syntax.  As far as I can see you don't use T2 anywhere, so I cannot say how you would need to structure your code to use it.

    Now a class template can take an instansiated template as one of its parameters, but it cannot actually take a template within a template.  For example:

    // using a function template here to minimize typing.  A class template would work fine as well.
    template <typename elementType, typename collectionType> 
    void AddToCollection(elementType const &element, collectionType &collection) {
        collection.push_back(element);
    }
    
    // another approach can be to use the template paramter in conjunction with an STL collection like:
    template<typename elementType>
    std::vector<elementType> CreateCollectionForItem(elementType const &item) {
        std::vector<elementType> newCollection;
        newCollection.push_back(item);
        return newCollection;
    }

    • Marked as answer by Shu 2017 Monday, November 24, 2014 12:44 PM
    Thursday, November 13, 2014 7:05 PM

All replies

  • You may need to elaborate on what you are trying to solve with this template in a template syntax.  As far as I can see you don't use T2 anywhere, so I cannot say how you would need to structure your code to use it.

    Now a class template can take an instansiated template as one of its parameters, but it cannot actually take a template within a template.  For example:

    // using a function template here to minimize typing.  A class template would work fine as well.
    template <typename elementType, typename collectionType> 
    void AddToCollection(elementType const &element, collectionType &collection) {
        collection.push_back(element);
    }
    
    // another approach can be to use the template paramter in conjunction with an STL collection like:
    template<typename elementType>
    std::vector<elementType> CreateCollectionForItem(elementType const &item) {
        std::vector<elementType> newCollection;
        newCollection.push_back(item);
        return newCollection;
    }

    • Marked as answer by Shu 2017 Monday, November 24, 2014 12:44 PM
    Thursday, November 13, 2014 7:05 PM
  • On 11/13/2014 1:30 PM, "Jafar Kumarkiri" wrote:

           Col<T, n> cc;

    The second parameter to Col is expected to be a class template, not an int. All your uses of Col<T, n> or Col<int, 5> are meaningless. You have it right in exactly one spot - this one:

        Col<int, vector> c;


    Igor Tandetnik
    Thursday, November 13, 2014 9:51 PM