我有这个容器:
class /*final*/ Row
{
public:
typedef FieldIterator const_iterator;
typedef FieldIterator iterator;
FieldIterator begin() const;
FieldIterator end() const;
FieldIterator begin();
FieldIterator end();
...
};
鉴于此,以下代码编译得很好:
BOOST_FOREACH(Field field, row)
{
}
但是,Row
该类不应具有可变迭代器,因此我通过删除可变访问来更改 Row 类:
class /*final*/ Row
{
public:
typedef FieldIterator const_iterator;
FieldIterator begin() const;
FieldIterator end() const;
...
};
但是现在相同的 foreach 循环无法编译:
1>o:\c\boost_1_48_0\boost\foreach.hpp(364): error C2039: 'type' : is not a member of 'boost::mpl::eval_if<C,F1,F2>'
1> with
1> [
1> C=boost::mpl::false_,
1> F1=boost::range_const_iterator<sqlserver::Row>,
1> F2=boost::range_mutable_iterator<sqlserver::Row>
1> ]
1> c:\dev\internal\playmssqlce\playmssqlce.cpp(29) : see reference to class template instantiation 'boost::foreach_detail_::foreach_iterator<T,C>' being compiled
1> with
1> [
1> T=sqlserver::Row,
1> C=boost::mpl::false_
1> ]
...
从错误消息中,我了解到BOOST_FOREACH
尝试实例化一个range_mutable_iterator
类型,这显然失败了。我如何让它实例化常量范围?
谢谢。
编辑
以下是Row
and的完整类声明FieldIterator
:
class /*final*/ Row
{
const BYTE *m_buffer;
const DBBINDING *m_pColumnBindings;
int m_columnBindingCount;
FieldIterator m_end;
public:
typedef FieldIterator const_iterator;
typedef FieldIterator iterator;
Row(const BYTE *buffer, const DBBINDING *pColumnBindings, int columnBindingCount);
bool isSameRow(const Row& r) const;
int fieldCount() const;
Field field(int i) const;
Field& field(int i, void *fieldBuffer) const;
FieldIterator begin() const;
FieldIterator end() const;
FieldIterator begin();
FieldIterator end();
};
class FieldIterator : public iterator_facade<FieldIterator, Field, boost::random_access_traversal_tag>
{
const Row *m_pRow;
int m_index;
mutable BYTE m_fieldBuffer[sizeof(Field)];
public:
FieldIterator(const Row *pRow = NULL, int index = 0);
private:
friend class boost::iterator_core_access;
void increment();
void decrement();
void advance(difference_type n);
difference_type distance_to(FieldIterator it);
reference dereference() const;
bool equal(const FieldIterator& rhs) const;
};