对boost::filtered_range值的引用类型
Type of reference to boost::filtered_range value
我想向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;
}
};
问题是使用什么类型作为运算符[]的返回类型?指定"value_type"不允许将类与"const"容器一起使用,"decltype(*begin())"不使用我的VC++2013进行编译。
您应该能够在基类上使用boost::range_reference<>
特性。
在Coliru上直播
#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)的,因此我为依赖的基成员和类型添加了必要的限定条件。
相关文章:
- 强制转换为引用类型
- 自定义引用类型
- Java 中是否有类似于 C++ 中引用类型"&"的内容?
- 为什么我不能在运算符=中使用引用类型?
- 可变参数模板函数参数和引用类型推导
- 隐式可转换参数,但属于引用类型
- 如何告诉自动推断向量<bool>元素的非引用类型
- 为什么"const auto [x, y]"绑定到引用类型时没有按预期运行?
- 引用类型的数据成员提供有关恒常正确性"loophole"
- 为什么引用类型在使用临时对象访问时是左值
- 什么是常量指针常量引用类型的参数?(const X* const & p)
- 在 lambda 中从引用类型捕获的值的类型,不使用通用捕获
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- void*作为通用引用类型是如何工作的
- 强制转换为不相关的引用类型是否违反严格的别名规则?
- C++ 引用类型作为递归函数参数
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 如果使用返回引用的函数初始化"auto"var,为什么它不声明引用类型?
- 为什么模板引用类型不能用作模板类型别名参数?
- 强制auto为range for循环中的引用类型