[英]Using boost counting_iterator with Incrementable type
I'm trying to use an Incrementable type with a boost::counting_iterator
. 我正在尝试使用带有
boost::counting_iterator
的Incrementable类型。
The boost::counting_iterator
documentation says that the iterator works for Incrementable types, ie types that are CopyConstructible, Assignable, PreIncrementable, and EqualityComparable. boost::counting_iterator
文档指出 ,迭代器适用于Incrementable类型,即CopyConstructible,Assignable,PreIncrementable和EqualityComparable的类型。
My Incrementable type: 我的递增类型:
template<class T> struct Incrementable {
// CopyConstructible:
Incrementable() : value(0) {}
Incrementable(const Incrementable& other) : value(other.value) {}
explicit Incrementable(const T& other) : value(other) {}
// Assignable:
inline Incrementable& operator=(const Incrementable& other) {
value = other.value;
return *this;
}
// PreIncrementable:
inline Incrementable& operator++() {
++value;
return *this;
}
// EqualityComparable:
friend
inline bool operator==(const Incrementable& a, const Incrementable& b) {
return a.value == b.value;
}
T value;
};
This fails to compile: 无法编译:
#include <boost/iterator/counting_iterator.hpp>
#include "incrementable.h"
int main() {
boost::counting_iterator<Incrementable<int>> a(Incrementable<int>(0));
return 0;
}
The error: 错误:
usr/local/include/boost/iterator/iterator_categories.hpp:161:60: error: no type named 'iterator_category' in 'boost::detail::iterator_traits<Incrementable<int> >'
typename boost::detail::iterator_traits<Iterator>::iterator_category
I'd guess that I need to implement an iterator_category either for: 我猜我需要为以下之一实现iterator_category:
Neither is clear from the documentation (which omits this topic completely), and I fail to find any information about it in the other parts of the library. 从文档中并不清楚(该文档完全省略了本主题),并且在库的其他部分中找不到关于它的任何信息。
So I added the following to boost::detail
namespace: 所以我添加了以下内容来
boost::detail
命名空间:
namespace boost { namespace detail {
template <class T> struct is_numeric<Incrementable<T>>
: mpl::true_ {};
}} // boost::detail namespace
and now everything compiles and works as expected. 现在一切都可以按预期进行编译和运行。 Still, I really doubt that the library was intended to be used this way.
不过,我真的怀疑该库是否打算以这种方式使用。
Anyone knows the proper/clean way to implement this? 任何人都知道正确的/干净的方法来实现这一目标吗?
Steve Jessop's suggestion: specializing std::numeric_limits
also works: 史蒂夫·杰索普(Steve Jessop)的建议:专门化
std::numeric_limits
也可以:
namespace std {
template<class T>
class numeric_limits<Incrementable<T>> : public numeric_limits<T> {
public:
static const bool is_specialized = true;
};
}
Still I don't know if this is the right thing to do for an incrementable type. 仍然我不知道这对于增量类型是否正确。
I'm not sure whether Boost defines "incrementable type" as you say. 我不确定Boost是否如您所说定义了“递增类型”。 If it does define it as you say, then there's a documentation bug, it shouldn't say that
counting_iterator
works for "any incrementable type", because those aren't the whole requirements. 如果确实按照您的定义进行了定义,则存在文档错误,它不应该说
counting_iterator
适用于“任何可增量类型”,因为这不是全部要求。 Or I suppose it's true if "provided that you specify the other template parameters correctly" goes without saying. 或者我想如果“只要您正确指定了其他模板参数”就不言而喻了。
The requirements on the Incrementable
template parameter for counting_iterator
are given in the document you link to (starting from " iterator_category
is defined as follows...", because the actual requirements section refers back to iterator_category
). 您链接到的文档中提供了关于
counting_iterator
的Incrementable
模板参数的要求(从“ iterator_category
定义如下...”开始,因为实际的要求部分参考了iterator_category
)。
You should not specialize boost::detail::is_numeric
. 您不应该专门使用
boost::detail::is_numeric
。 You should specialize std::numeric_limits
. 您应该专门研究
std::numeric_limits
。 But in your example, I think you've actually narrowed the interface of T
too much to claim that your type is numeric (it doesn't have arithmetic). 但是在您的示例中,我认为您实际上缩小了
T
的接口的范围,以至于不能声称您的类型是数字的(它没有算术)。 If your type is neither numeric nor an iterator, I think you're supposed to specify CategoryOrTraversal
as forward_iterator_tag
. 如果您的类型既不是数字类型也不是迭代器,那么我认为您应该将
CategoryOrTraversal
指定为forward_iterator_tag
。 I may have missed something. 我可能错过了一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.