2

boost多数组迭代器中是否缺少箭头运算符?我期望这行得通是错的吗?

#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;
}
4

1 回答 1

2

似乎是一个错误。array_iterator::operator->返回一个:

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

在哪里:

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_;
};

但是T*在这里foo&*,您不能将指针指向引用。此外,您不能有mutable参考成员。因此,对于这个用例,整个类模板都被破坏了。

于 2016-08-11T15:29:50.060 回答