【问题标题】:Why doesn't the arrow operator "->" work on boost::numeric::ublas::vector<...>::iterator?为什么箭头运算符“->”在 boost::numeric::ublas::vector<...>::iterator 上不起作用?
【发布时间】:2014-10-20 09:22:36
【问题描述】:

考虑这段代码:

struct CData
{
    int bar() { return 1; }
};

int main()
{
    typedef boost::numeric::ublas::vector<CData> vec_data_t;

    vec_data_t foo;
    for (vec_data_t::iterator it = foo.begin();
         it != foo.end();
         ++it)
    {
        std::cout << it->bar() << std::endl;    // COMPILE ERROR!
        std::cout << (*it).bar() << std::endl;  // ok
    }

    return 0;
}

为什么循环中使用箭头运算符的第一行编译失败,而使用运算符*的下一行可以正常编译? 我习惯于将箭头运算符与 std 容器迭代器一起使用,并想知道为什么它会因 boost::numeric::ublas 迭代器而失败。

我正在使用 boost 1.54 和 gcc 4.9.1,确切的错误消息是:

 error: base operand of ‘->’ has non-pointer type ‘boost::numeric::ublas::vector<CData>::iterator’

【问题讨论】:

    标签: c++ boost boost-ublas


    【解决方案1】:

    我会说这是 Boost 中的一个错误。根据文档,boost::numeric::ublas::vector 模拟了一个 Vector Expression,它提供了一个 Indexed Bidirectional iterator,其中(除其他外)将以下内容列为有效表达式及其语义:

    会员访问 | it-&gt;m | 类型要求 T 是为其定义了t.m 的类型。


    会员访问 | it-&gt;m | 前提条件 it 是可取消引用的。 | 语义相当于(*it).m

    查看代码显示indexed_iterator 类模板没有定义operator-&gt;。根据文档,它显然应该。

    您可能想查找错误报告,如果没有,请提交一份。

    【讨论】:

      【解决方案2】:

      因为operator-&gt;()是一个极难实现的运算符。

      我见过的任何重要的自定义迭代器都没有实现-&gt;。 我的猜测是 uBlas 的实现者不知道该怎么做(我不怪他们)。

      问题当然是要保证-&gt;链的末尾有一个指针,并且在该指针之上it-&gt;bla(*it).bla具有相同的含义

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 2013-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多