tango-1
tango-1

Reputation: 350

A template parameter works with different STL containers?

I have an question and I cant find any solution about this. I am writing a Java Collection Hierarchy using c++ stl containers. And I have Templated class Collection which is base abstract class and List class that derives from Collecton and ArrayList Class which derives from List.

Interfaces are :

template<typename T,template <typename...> class Container>
class collections
{
public:
    virtual gtuiterator<T,Container> iterator()=0;//my own iterator class
    virtual bool contains(T e)=0;
    virtual bool containsAll(collections& c)=0;
    //and other methods 
};

List.h same as the Collections.h

template<typename T,template <typename...> class Container>
class list : public collections<T,Container>
{
public:
    virtual gtuiterator<T,Container> iterator()=0;
    virtual bool contains(T e)=0;
    virtual bool containsAll(collections<T,Container>& c)=0;        
};

And ArrayList.h

template<typename T,template <typename...> class Container>
class ArrayList : public list<T,Container>
{
public:
    virtual gtuiterator<T,Container> iterator()override{
        gtuiterator<T,Container> iter(&array);
        return iter;
    }
public:
    ...
};

And In the main I call containsAll method :

ArrayList<int,std::list> test1;
for (int i = 0; i < 5; i++)
    test1.add(i);

ArrayList<int,std::vector> test2;
for (int i = 0; i < 5; ++i)
    test2.add(i);

cout << "TESTED =" << test1.containsAll(test2) << endl;

And compiler says that there is no conversion from bla bla..

        error: no matching function for call to ‘collectionsgtu::ArrayList<int, std::__cxx11::list>::containsAll(collectionsgtu::ArrayList<int, std::vector>&)’
      cout << "TESTED =" << test1.containsAll(test2) << endl;
                                                   ^
    In file included from test.cpp:2:0:
    arraylist.h:34:16: note: candidate: bool collectionsgtu::ArrayList<T, Container>::containsAll(collectionsgtu::collections<T, Container>&) [with T = int; Container = std::__cxx11::list]
       virtual bool containsAll(collections<T,Container>& c)override{
arraylist.h:34:16: note:   no known conversion for argument 1 from ‘collectionsgtu::ArrayList<int, std::vector>’ to ‘collectionsgtu::collections<int, std::__cxx11::list>&’

I know the reason of error, for example when I declare test2<int,std::vector>, Compiler puts std::vector by T, so containsAll function parameter becomes a std::vector parameter and does not accept other stl containers. If the second parameter of the test1 and test2 is the same, code works.

How Can I template theese classes that accepts some stl containers(set,list,vector) for function parameters like containAll ?

Thanks.

Upvotes: 1

Views: 365

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

You could make containsAll a template function:

template<typename C>
bool containsAll(collection<T, C> const&);

Since template member functions can't be virtual you have to implement it in the collection class, but it can be implemented to call some protected virtual function.

Upvotes: 1

Related Questions