Для чего-то подобного, я бы использовал класс политики, чтобы определить, как вещи вставляются в один список, например,
struct DefaultInsertionPolicy
{
template <typename ContainerType, typename ValueType>
static void insert(ContainerType& container, ValueType const& cValue)
{
container.insert(container.end(), cValue);
}
};
struct SortedInsertionPolicy
{
template <typename ContainerType, typename ValueType>
static void insert(ContainerType& container, ValueType const& cValue)
{
// I'm using lower_bound here, but do what is necessary for you
typename ContainerType::iterator ft = std::lower_bound(container.begin(), container.end(), cValue);
container.insert(ft, cValue);
}
};
template <typename T, typename InsertPolicy = DefaultInsertionPolicy>
class List
{
:
// insert function
void insert(T const& cValue)
{
InsertPolicy::insert(*this, cValue); // delegate to the policy to do the insert
}
void insert(iterator iPos, T const& cValue)
{
// do the real insertion at the provided position.
}
};
Таким образом, реальные типы могут быть
typedef List<some_type> UList; // insertion order
typedef List<some_type, SortedInsertionPolicy> OList; // sorted list