我正在尝试为我的基类 Iterator 编写 LINQ 样式的方法,List 和 Sequence 将从该基类继承,但是这两个容器将有自己的这些方法的实现。“Where”方法非常简单。“选择”方法非常棘手;你不能有虚拟模板方法。
template <typename T>
class Iterator {
public:
virtual ~Iterator() {};
// This is illegal, but if it weren't, it would be the functionality I want.
template <typename R>
virtual shared_ptr<IIterator<R>> Select(const function<R(T)>& func) = 0;
virtual shared_ptr<IIterator<T>> Where(const function<bool(T)>& func) = 0;
};
例如,“选择”将允许您将“火腿三明治”类型的迭代器转换为“生菜”类型的迭代器。
HamSandwiches->Select<'Lettuce'>([] (shared_ptr<'HamSandwich'> hs) { return hs->Lettuce; });
忽略单引号。
因为我们不能有虚拟模板函数,所以我当然不能把函数设为虚拟。在这种情况下,我们有一个普通的旧函数,与虚函数相比,我们永远不应该通过在 List 和 Sequence 中编写实现来“隐藏”它的实现;这将被视为设计缺陷。
template <typename T>
class Iterator {
public:
virtual ~Iterator() {};
template <typename R>
shared_ptr<Iterator<R>> Select(const function<R(T)>& func);
virtual shared_ptr<Iterator<T>> Where(const function<bool(T)>& func) = 0;
};
template <typename T>
template <typename R>
shared_ptr<Iterator<R>> Iterator<T>::Select(const function<R(T)>& func) {
//Implementation - What would it be?
}
现在我们必须在我们的基类中有实现,这个实现需要特定于 List 和 Sequence。从我所见,您将开始创建受保护的“实现函数”以在“选择”中执行某些可能被列表或序列覆盖的操作。
我不是在这里寻找确切的答案,我正在寻找可以帮助我到达我可能/应该想去的地方的东西。有没有人发现任何常见的陷阱或我可能做错的事情?