C++ boost::filtered_范围值的引用类型

C++ boost::filtered_范围值的引用类型,c++,boost,C++,Boost,我想在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 &Rang

我想在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;
    }
};
模板
类my_filtered_range:public boost::filtered_range
{
公众:
my_filtered_range(预测谓词、TRange和range):boost::filtered_range(谓词、范围)
{
}
大小\u t大小()常量
{
返回标准::距离(开始(),结束());
}
运算符[](大小索引)常数
{
断言(索引
问题是使用什么类型作为运算符[]的返回类型?
指定“value\u type”不允许将类与“const”容器一起使用,“decltype(*begin())”不使用我的VC++2013进行编译。

您应该能够在基类上使用
boost::range\u reference
特性

#包括
模板
类my_filtered_range:public boost::filtered_range
{
公众:
typedef boost::过滤范围基本类型;
my_filtered_range(预测谓词、TRange和range):boost::filtered_range(谓词、范围)
{
}
大小\u t大小()常量
{
返回std::distance(this->begin(),this->end());
}
typename boost::range\u reference::type运算符[](大小索引)常量
{
断言(索引size());
auto It=此->开始();
std::前进(It,索引);
归还它;
}
};

请注意,我是如何检测到您正在使用一个已损坏的编译器(MSVC),因此我为依赖的基本成员和类型添加了必要的限定条件。

您是否尝试过在尾部返回类型中使用
decltype
进行
auto
?感谢您的解决方案。“base_type”是私有的,我使用boost::range_引用,它可以工作。Erm@掠袭者,
base_类型
不是私有的。我在自己的示例中定义了它!你可能会假设它对于基础的rangem是相同的,但是为什么要假设你能使它一般正确呢?明白了!对不起,我疏忽了。
#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;
    }
};