箭头操作符和boost多阵列迭代器

时间:2016-08-11 15:00:52

标签: c++ boost boost-multi-array

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

1 个答案:

答案 0 :(得分: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参考成员。因此,这个完整的类模板只是为这个用例打破了。