我有一些类,由于各种原因,这些类超出了本讨论的范围,因此无法修改(省略了相关的实现细节):
class Foo { /* ... irrelevant public interface ... */ };
class Bar {
public:
Foo& get_foo(size_t index) { /* whatever */ }
size_t size_foo() { /* whatever */ }
};
(我正在处理许多类似的“Foo”和“Bar”类,它们都是从其他地方生成的代码以及我不想子类化的东西,等等。)
[编辑:澄清-尽管有许多相似的'Foo'和'Bar'类,但可以确保每个“外部”类都具有getter和size方法。根据每个“内部”包含类型的不同,对于每个“外部”,只有getter方法名称和返回类型会有所不同。
因此,如果我有包含Quux实例的Baz,将有Quux&Baz::get_quux(size_t index)和size_t Baz::size_quux()。]
给定Bar类的设计,您不能轻松地在STL算法中使用它(例如for_each,find_if等),并且必须进行命令式循环而不是采用功能性方法(为什么我更喜欢后者,这也超出了范围)此讨论):
Bar b;
size_t numFoo = b.size_foo();
for (int fooIdx = 0; fooIdx < numFoo; ++fooIdx) {
Foo& f = b.get_foo(fooIdx);
/* ... do stuff with 'f' ... */
}
所以...我从来没有创建过自定义迭代器,而是在阅读了有关S.O的各种问题/答案之后。关于iterator_traits之类的东西,我想出了这个(目前半熟)的“解决方案”:
首先,自定义迭代器机制(注意:'function'和'bind'的所有用法均来自MSVC9中的std::tr1):
// Iterator mechanism...
template <typename TOuter, typename TInner>
class ContainerIterator : public std::iterator<std::input_iterator_tag, TInner> {
public:
typedef function<TInner& (size_t)> func_type;
ContainerIterator(const ContainerIterator& other) : mFunc(other.mFunc), mIndex(other.mIndex) {}
ContainerIterator& operator++() { ++mIndex; return *this; }
bool operator==(const ContainerIterator& other) {
return ((mFunc.target<TOuter>() == other.mFunc.target<TOuter>()) && (mIndex == other.mIndex));
}
bool operator!=(const ContainerIterator& other) { return !(*this == other); }
TInner& operator*() { return mFunc(mIndex); }
private:
template<typename TOuter, typename TInner>
friend class ContainerProxy;
ContainerIterator(func_type func, size_t index = 0) : mFunc(func), mIndex(index) {}
function<TInner& (size_t)> mFunc;
size_t mIndex;
};
接下来,通过这种机制,我可以获得代表内部容器的开始和结束的有效迭代器:
// Proxy(?) to the outer class instance, providing a way to get begin() and end()
// iterators to the inner contained instances...
template <typename TOuter, typename TInner>
class ContainerProxy {
public:
typedef function<TInner& (size_t)> access_func_type;
typedef function<size_t ()> size_func_type;
typedef ContainerIterator<TOuter, TInner> iter_type;
ContainerProxy(access_func_type accessFunc, size_func_type sizeFunc) : mAccessFunc(accessFunc), mSizeFunc(sizeFunc) {}
iter_type begin() const {
size_t numItems = mSizeFunc();
if (0 == numItems) return end();
else return ContainerIterator<TOuter, TInner>(mAccessFunc, 0);
}
iter_type end() const {
size_t numItems = mSizeFunc();
return ContainerIterator<TOuter, TInner>(mAccessFunc, numItems);
}
private:
access_func_type mAccessFunc;
size_func_type mSizeFunc;
};
我可以通过以下方式使用这些类:
// Sample function object for taking action on an LMX inner class instance yielded
// by iteration...
template <typename TInner>
class SomeTInnerFunctor {
public:
void operator()(const TInner& inner) {
/* ... whatever ... */
}
};
// Example of iterating over an outer class instance's inner container...
Bar b; /* assume populated which contained items ... */
ContainerProxy<Bar, Foo> bProxy(
bind(&Bar::get_foo, b, _1),
bind(&Bar::size_foo, b));
for_each(bProxy.begin(), bProxy.end(), SomeTInnerFunctor<Foo>());
从经验上讲,此解决方案可以正常工作(为简洁起见,请删除我在编辑上述内容时可能引入的任何复制/粘贴或错字)。
因此,最后是实际问题:
我不喜欢要求调用者使用bind()和_1占位符等。他们真正关心的是:外部类型,内部类型,外部类型的获取内部实例的方法,外部类型的获取计数内部实例的方法。
有什么办法可以某种方式“隐藏”模板类主体中的绑定(bind)吗?我一直无法找到一种为类型和内部方法分别提供模板参数的方法...
谢谢!
大卫
最佳答案
或者,如果函数具有可预测的签名,则可以始终将函数本身用作模板参数:
template <typename TOuter, typename TInner,
TInner& (TOuter::*getfunc)(size_t )>
class ContainerIterator
{
public:
//...
TInner& operator*() {return mContainerRef.*getfunc(mIndex);}
//...
};
template <typename TOuter, typename TInner,
size_t (TOuter::*sizefunc)(),
TInner& (TOuter::*getfunc)(size_t )>
class ContainerProxy
{
public:
//...
ContainerIterator<TOuter, TInner, getfunc> end() {
return ContainerIterator<TOuter, TInner, getfunc>
(mContainerRef,
mContainerRef.*sizefunc());
}
//...
};
int main()
{
Bar b;
ContainerProxy<Bar, Foo, &Bar::size_foo, &Bar::get_foo> proxy(b);
std::for_each(proxy.begin(), proxy.end(), /*...*/);
}
或者走得更远(http://ideone.com/ulIC7)并通过将函数包装在
std::function
中来传递这些函数:template <typename TOuter, typename TInner>
struct ContainerIterator : public std::iterator<std::input_iterator_tag, TInner>
{
TOuter& mContainerRef;
//...
typedef std::function<TInner& (TOuter*, size_t)> getfunc_type;
getfunc_type mGetfunc;
ContainerIterator(TOuter& containerRef, size_t index, getfunc_type const& getFunc)
/* ... */ {}
TInner& operator*() {return mGetfunc(&mContainerRef, mIndex);}
ContainerIterator<TOuter, TInner>& operator++() {++mIndex; return *this;}
// ...
};
template <typename TOuter, typename TInner>
struct ContainerProxy
{
TOuter& mContainerRef;
typedef std::function<size_t (TOuter*)> sizefunc_type;
sizefunc_type mSizefunc;
typedef std::function<TInner& (TOuter*, size_t)> getfunc_type;
getfunc_type mGetfunc;
ContainerProxy(TOuter& containerRef, sizefunc_type sizefunc, getfunc_type getfunc)
// ...
{
}
// ...
ContainerIterator<TOuter, TInner> end() const
{
return ContainerIterator<TOuter, TInner>(mContainerRef,
mSizefunc(&mContainerRef),
mGetfunc);
}
};
int main()
{
Bar b=...;
ContainerProxy<Bar, Foo> proxy(b, &Bar::size_foo, &Bar::get_foo);
std::for_each(proxy.begin(), proxy.end(), /*...*/);
}
关于c++ - 调整非可迭代容器以通过自定义模板化迭代器进行迭代,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11165341/