#include <iostream>
#include <vector>
#include <list>
using namespace std;
// demo class
class Item {
int i;
public:
Item(int v=0) : i(v) {}
friend ostream& operator<< (ostream& os, const Item &item);
};
ostream& operator<< (ostream& os, const Item &item) {
return (os << item.i);
}
template <template <typename...> class C, class T>
class Adder {
public:
void operator() (C<T>& a, const T& element);
};
template <class T>
class Adder<vector, T> {
public:
void operator() (vector<T>& a, const T& element) {
a.push_back(element);
}
};
template <class T>
class Adder<list, T> {
public:
void operator() (list<T>& a, const T& element) {
a.push_front(element);
}
};
template <template <typename...> class C, class T>
class SmartContainer {
C<T> mycontainer;
Adder<C,T> a;
public:
void add (const T& element) { a(mycontainer, element); }
int get_number_of_elements() { return mycontainer.size(); }
void print() { for (auto &x:mycontainer)
cout << x<<endl;
cout<<endl; }
};
int main() {
Item item1(10), item2(20);
SmartContainer<vector, Item> c1;
c1.add(item1);
c1.add(item2);
cout<< "Size: "<<c1.get_number_of_elements()<<endl;
c1.print();
SmartContainer<list, Item> c2;
c2.add(item1);
c2.add(item2);
cout<< "Size: "<<c2.get_number_of_elements()<<endl;
c2.print();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bGlzdD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIGRlbW8gY2xhc3MgCmNsYXNzIEl0ZW0gewoJaW50IGk7IApwdWJsaWM6IAogICAgSXRlbShpbnQgdj0wKSA6IGkodikge30KICAgIGZyaWVuZCBvc3RyZWFtJiBvcGVyYXRvcjw8IChvc3RyZWFtJiBvcywgY29uc3QgSXRlbSAmaXRlbSk7IAp9Owpvc3RyZWFtJiBvcGVyYXRvcjw8IChvc3RyZWFtJiBvcywgY29uc3QgSXRlbSAmaXRlbSkgewoJcmV0dXJuIChvcyA8PCBpdGVtLmkpOyAKfSAKCnRlbXBsYXRlIDx0ZW1wbGF0ZSA8dHlwZW5hbWUuLi4+IGNsYXNzIEMsIGNsYXNzIFQ+CmNsY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