#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
// Item
class MyA { };
class MyB { };
template <class T>
class MyItem
{
public:
void Print();
};
template <>
void MyItem<MyA>::Print()
{
cout << "MyItem - MyA" << endl;
}
template <>
void MyItem<MyB>::Print()
{
cout << "MyItem - MyB" << endl;
}
// ItemList
class MyItemList
{
public:
template <typename T>
void AddItem(MyItem<T> theItem);
void PrintItems();
private:
template <typename T>
list<MyItem<T>> & GetTypedList();
list<MyItem<MyA>> myAList;
list<MyItem<MyB>> myBList;
};
template <typename T>
void MyItemList::AddItem(MyItem<T> theItem)
{
GetTypedList<T>().push_back(theItem);
}
template <>
list<MyItem<MyA>> & MyItemList::GetTypedList()
{
return myAList;
}
template <>
list<MyItem<MyB>> & MyItemList::GetTypedList()
{
return myBList;
}
void MyItemList::PrintItems()
{
// Reihenfolge ist uns mal egal
for_each(myAList.cbegin(), myAList.cend(), [](MyItem<MyA> i)
{
i.Print();
});
for_each(myBList.cbegin(), myBList.cend(), [](MyItem<MyB> i)
{
i.Print();
});
}
// main
int main(int argc, char * argv[])
{
MyItemList itemList;
// itemList soll die Elemente tatsächlich besitzen
{
itemList.AddItem(MyItem<MyA>());
itemList.AddItem(MyItem<MyB>());
}
itemList.PrintItems();
return 0;
}
CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGxpc3Q+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gSXRlbQpjbGFzcyBNeUEgeyB9OwpjbGFzcyBNeUIgeyB9OwoKdGVtcGxhdGUgPGNsYXNzIFQ+CmNsYXNzIE15SXRlbQp7CnB1YmxpYzoKCXZvaWQgUHJpbnQoKTsKfTsKCnRlbXBsYXRlIDw+CnZvaWQgTXlJdGVtPE15QT46OlByaW50KCkKewoJY291dCA8PCAiTXlJdGVtIC0gTXlBIiA8PCBlbmRsOwp9Cgp0ZW1wbGF0ZSA8Pgp2b2lkIE15SXRlbTxNeUI+O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