Link to the mcve.
We define a matrix to be iterable both by rows and by columns. Here's the implementation of the row-wise iterator:
template<class Real>
class RowIterator {
public:
RowIterator() { }
RowIterator(Real* begin, size_t rows, size_t cols) : begin(begin), rows(rows), cols(cols) { }
Real* operator*() const { return begin; }
Real& operator[](size_t col) const { return begin[col]; }
bool operator!=(const RowIterator& it) const { return begin != it.begin; }
RowIterator& operator++() { begin += cols; --rows; return *this; }
private:
Real* begin;
size_t rows, cols;
};
Iterating over our matrix is implemented using a Range
object define as follows:
namespace details
{
template<class Iterator>
struct Range {
Iterator begin, end;
Range() { }
Range(Iterator begin, Iterator end) : begin(begin), end(end) { }
};
template<class Iterator>
Iterator begin(const Range<Iterator>& range) { return range.begin; }
template<class Iterator>
Iterator end(const Range<Iterator>& range) { return range.end; }
}
using details::Range;
template<class Iterator>
Range<Iterator> make_range(Iterator begin, Iterator end) { return Range<Iterator>(begin, end); }
This is basically our usage code:
Range<RowIterator<float>> make_row_range(float* mat, size_t rows, size_t cols) {
return make_range(
RowIterator<float>(mat, rows, cols),
RowIterator<float>(mat + rows * cols, 0, cols));
}
int main() {
size_t rows = 4, cols = 6;
float* mat = new float[rows * cols];
for(size_t i = 0; i < rows * cols; ++i)
mat[i] = (float)i;
auto rowrange = make_row_range(mat, rows, cols);
// this loop works as expected
std::cout << "begin, end" << std::endl;
for(auto b = begin(rowrange), e = end(rowrange); b != e; ++b) {
// using RowIterator<T>::operator[](size_t)
std::cout << "start of row: " << b[0] << std::endl;
}
// this loop produces confusing compiler errors
std::cout << "range based" << std::endl;
for(auto row : rowrange) { // this is line 42
// row is of type float*
std::cout << "start of row: " << row[0] << std::endl;
}
return 0;
}
I compiled the above MCVE and got the following compiler errors:
Visual Studio 2013 (all on line 42):
error C2064: term does not evaluate to a function taking 0 arguments
error C3536: '$S2': cannot be used before it is initialized
error C3536: '$S3': cannot be used before it is initialized
error C2100: illegal indirection
error C2440: 'initializing' : cannot convert from 'int' to 'float *'
GCC 5.1 (on line 42):
error: no match for call to '(RowIterator<float>) ()'
Clang 3.7.0 (on line 42):
error: type 'RowIterator<float>' does not provide a call operator
note: when looking up 'begin' function for range expression of type 'details::Range<RowIterator<float> >'
All compilers are searching for a call operator. Why? As I understand, the above iterator provides the minimal interface for ranged loops and it works when using the syntactical equivalence code from cppreference.com.
While writing this question I came up with the solution (rubber SO debugging?): the compiler first checks for the members Range::begin
and Range::end
and tries to invoke those leading to the missing call operator. None of the tested compilers indicated this clearly in their error messages[1]. The fix is to simply rename them:
namespace range
{
template<class Iterator>
struct Range {
// "begin" and "end" have ultra-special meaning in this context!!!
Iterator range_begin, range_end;
Range() { }
Range(Iterator begin, Iterator end) : range_begin(begin), range_end(end) { }
};
template<class Iterator>
Iterator begin(const Range<Iterator>& range) { return range.range_begin; }
template<class Iterator>
Iterator end(const Range<Iterator>& range) { return range.range_end; }
}
The requirements on class Range
are well defined (source: cppreference.com, emphasis mine):
begin_expr and end_expr are defined as follows:
1 If range_expression is an expression of array type, then begin_expr is
__range
and end_expr is(__range + __bound)
, where__bound
is the number of elements in the array (if the array has unknown size or is of an incomplete type, the program is ill-formed)2 If range_expression is an expression of a class type C that has a member named
begin
and/or a member namedend
(regardless of the type or accessibility of such member), then begin_expr is__range.begin()
and end_expr is__range.end()
;3 Otherwise, begin_expr is
begin(__range)
and end_expr isend(__range)
, which are found via argument-dependent lookup (non-ADL lookup is not performed).
[1]: Clang actually came close, though even its message is ambiguous: I thought it was (adl) looking up details::begin(Range)
instead it was looking straight at Range::begin
.