我有自己的工作迭代器来水平或垂直遍历图像中的像素。通过模板参数,它可能是也可能不是( Dobb 博士的const
巧妙技巧)。
然后我意识到有一个std::iterator
基类,我想我会让我的东西更加STLly并从它继承。
不幸的是,现在Visual Studio 2012(版本 11.0.60315.01 更新 2)将不再编译它。我实际上设法获得了编译器stackoverflow。这是消息:
错误 1 错误 C1063:编译器限制:编译器堆栈溢出 d:\...\source.cpp 42 1 ConsoleApplication3
我的班级的一个非常精简的版本如下所示:
#include <iterator>
// This comes from the outer image class.
typedef float color_type;
template<bool IS_CONST = false>
struct Iterator : public std::iterator<std::random_access_iterator_tag, color_type>
{
// This is myself...
typedef Iterator<IS_CONST> iterator;
// ... and my variants.
typedef Iterator<true> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
// Make base class typedefs available.
typedef typename iterator::value_type value_type;
typedef typename iterator::difference_type difference_type;
// My own typedefs to deal with immutability.
typedef value_type const & const_reference;
typedef typename std::conditional<IS_CONST, const_reference, typename iterator::reference>::type reference;
Iterator()
: _position(nullptr),
_step()
{
}
Iterator(value_type * const position, difference_type const step)
: _position(position),
_step(step)
{
}
iterator const operator-(difference_type n) const
{
return iterator(*this) -= n;
}
difference_type operator-(iterator const rhs) const
{
assert(_step == rhs._step);
return (_position - rhs._position) / _step;
}
protected:
value_type * _position;
difference_type _step;
};
int main()
{
float a = 3.141f;
// Instanciate all variants.
Iterator<false> empty;
Iterator<true> const_empty;
Iterator<false> some(&a, 5);
Iterator<true> const_some(&a, 5);
return 0;
}
删除两者中的任何一个operator-
都会使编译器满意。
有人可以向我解释这里的问题吗?或者甚至更好地为它提供修复?
谢谢
更新:哦,顺便说一句,GCC 4.7.2 愉快地编译了它。