箭头操作符和Boost MultiArray迭代器

3
boost multiarray迭代器中缺少箭头运算符吗?我期望它能正常工作,这个想法有问题吗?
#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;
}

出现了什么类型的编译错误? - Sean Kuhlman
boost\multi_array\iterator.hpp 中的翻译内容为:'->': 指向引用的指针是非法的 - cambunctious
1个回答

2
似乎是一个bug。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&*,你不能获取一个引用的指针。此外,你也不能有一个可变的引用成员。所以,对于这种情况,整个类模板都是错误的。


糟糕,这个问题能轻松解决吗? - cambunctious
@cambunctious 我假设是的。 - Barry
@cambunctious arrow_proxy 的 hack 可以行得通,问题是多维数组需要通用地实现任何维度的东西(或者使 1D 成为特殊情况,这违背了维度通用的目的)。在我的经验中,在任何维度上实现 -> 是可能的,但非常困难,我花了一段时间才在我的库中使其工作,可以在这里看到 https://godbolt.org/z/zGe5Kcar4 。如果出于通用编程原因,您确实需要 operator->() 工作,您可以尝试使用我的库。 - alfC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接