作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想添加一个 operator[] 到 boost::filtered_range 类。这是我的代码:
template <typename TPredicate, typename TRange>
class my_filtered_range : public boost::filtered_range<TPredicate, TRange>
{
public:
my_filtered_range(TPredicate Predicate, TRange &Range) : boost::filtered_range<TPredicate, TRange>(Predicate, Range)
{
}
size_t size() const
{
return std::distance(begin(), end());
}
???? &operator[](size_t Index) const
{
assert(Index < size());
auto It = begin();
std::advance(It, Index);
return *It;
}
};
问题是使用什么类型作为 operator[] 的返回类型?指定“value_type”不允许将类与“const”容器一起使用,“decltype(*begin())”不能用我的 VC++2013 编译。
最佳答案
您应该能够使用 boost::range_reference<>
基类的特征。
#include <boost/range/adaptors.hpp>
template <typename TPredicate, typename TRange>
class my_filtered_range : public boost::filtered_range<TPredicate, TRange>
{
public:
typedef boost::filtered_range<TPredicate, TRange> base_type;
my_filtered_range(TPredicate Predicate, TRange &Range) : boost::filtered_range<TPredicate, TRange>(Predicate, Range)
{
}
size_t size() const
{
return std::distance(this->begin(), this->end());
}
typename boost::range_reference<const base_type>::type operator[](size_t Index) const
{
assert(Index < this->size());
auto It = this->begin();
std::advance(It, Index);
return *It;
}
};
请注意我是如何检测到您使用的是损坏的编译器 (MSVC),因此我向依赖的基本成员和类型添加了必要的限定条件。
关于c++ - boost::filtered_range 值的引用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26407479/
我想添加一个 operator[] 到 boost::filtered_range 类。这是我的代码: template class my_filtered_range : public boost
我是一名优秀的程序员,十分优秀!