Why does std::span overload the function call operator for indexing?

It is there because mdspan, a not-yet-accepted multi-dimensional span type, uses operator() for indexing. After all, operator[] only takes one index, while mdspan needs multiple indexing.

So for the sake of allowing these two types to have as similar an interface as possible, span also allows operator().

Note that using operator() is a common convention in C++ for multi-dimensional indexing. Eigen and Boost both use it, as do many others.


From the relevant proposal:

span also overloads operator() for element access, to provide compatibility with code written to operate against view.

The view has been renamed to mdspan by now, which is not standardized yet.

As correctly noted in Nicol Bolas' answer, mdspan will use operator() to accept multiple indices.

Tags:

C++

C++20