Search code examples
c++boostboost-multi-array

arrow operator and boost multiarray iterator


Is the arrow operator missing from boost multiarray iterators? Am I wrong to expect this to work?

#include <vector>
#include <boost/multi_array.hpp>

struct foo {
    int n;
};

int main()
{
    {
        std::vector<foo> a;
        auto it = a.begin();
        int test = it->n; // this does compile
    }

    {
        boost::multi_array<foo, 1> a;
        auto it = a.begin();
        int test = it->n; // this does not compile
    }
    return 0;
}

Solution

  • Seems like a bug. array_iterator::operator-> returns a:

    // reference here is foo&
    operator_arrow_proxy<reference> operator->() const;
    

    Where:

    template <class T>
    struct operator_arrow_proxy
    {
      operator_arrow_proxy(T const& px) : value_(px) {}
      T* operator->() const { return &value_; }
      // This function is needed for MWCW and BCC, which won't call operator->
      // again automatically per 13.3.1.2 para 8
      operator T*() const { return &value_; }
      mutable T value_;
    };
    

    But T* here would be foo&* and you can't take a pointer to a reference. Moreover you cannot have a mutable reference member. So this whole class template is just broken for this use case.