I am trying to create my own "smart iterator" and I'd like to use SFINAE to have some operators depending on the tag of the iterator :
Here is my code :
template<class Iterator, class Predicat, class Tag>
class RangeFilterIterator {
public:
RangeFilterIterator(Iterator begin, Iterator end, Predicat predicat) :
mBegin(begin), mEnd(end), mPredicat(predicat) {}
bool operator !=(RangeFilterIterator const &r) {
return mBegin != r.mBegin;
}
typename Iterator::value_type &operator*() {return *mBegin;}
RangeFilterIterator &operator++() {
while(mBegin != mEnd && mPredicat(*mBegin++));
return *this;
}
template<class = std::enable_if_t<std::is_base_of<std::random_access_iterator_tag, Tag>::value>>
RangeFilterIterator &operator+(std::size_t n) {
while(n--)
++(*this);
return *this;
}
template<class = std::enable_if_t<!std::is_base_of<std::random_access_iterator_tag, Tag>::value>>
RangeFilterIterator &operator+(std::size_t n) = delete;
private:
Iterator mBegin, mEnd;
Predicat mPredicat;
};
template<typename Container, typename Predicate>
auto RangeFilter(Container const &c, Predicate p) {
using Iterator = RangeFilterIterator<typename Container::iterator,
Predicate,
typename Container::iterator::iterator_category>;
Iterator begin(const_cast<Container&>(c).begin(), const_cast<Container&>(c).end(), p);
Iterator end(const_cast<Container&>(c).end(), const_cast<Container&>(c).end(), p);
return Range(begin, end);
}
and at the line RangeFilterIterator &operator+(std::size_t n) = delete
I got the error : class member cannot be redeclared
.
I am not "good" with template, but I thought that with SFINAE only one of the two will be "declared". Am I missing something? It is possible to do otherwise?
Okay when I am using return type parameter instead of template parameter, it works.
template<class tag = Tag>
std::enable_if_t<std::is_base_of<std::random_access_iterator_tag, tag>::value, RangeFilterIterator>
&operator+(std::size_t n) {
while(n--)
++(*this);
return *this;
}
template<class tag = Tag>
std::enable_if_t<!std::is_base_of<std::random_access_iterator_tag, tag>::value, RangeFilterIterator>
&operator+(std::size_t n) = delete;