4

尝试在迭代器类型中使用 -> 时遇到一些错误。当我在定义迭代器的库中进行挖掘时,在我看来一切都很好,并且没有任何错误的原因。这是代码,boost::multi_array 的一部分:

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

const std::pair<double, unsigned int>&;实例化 然后编译器抱怨“形成指向引用类型 'const std::pair<double, unsigned int>&' 的指针”。这些是内部的库实例。作为记录,这是的代码中的内容:

typedef uint32_t object_indentifier_t;
typedef std::pair< double, object_identifier_t > object_tab_t;
typedef boost::multi_array< object_tab_t, 2 > index_t;

这是引起麻烦的用法:

object_identifier const& center; // Actually a parameter
index_t::const_subarray<1>::type::const_iterator pos_iterator_left = std::lower_bound( ix[i].begin(), ix[i].end(), sk[i], comparer ); 
assert( pos_iterator_left -> second == center ); // <-- Error steams from here

这是更多错误上下文:

/opt/boost_1_48_0/include/boost/multi_array/iterator.hpp: In instantiation of 'struct boost::detail::multi_array::operator_arrow_proxy<const std::pair<double, unsigned int>&>':
csrc/lsh_cpp/lsh.cpp|125 col 13| required from here
/opt/boost_1_48_0/include/boost/multi_array/iterator.hpp|40 col 10| error: forming pointer to reference type 'const std::pair<double, unsigned int>&'
/opt/boost_1_48_0/include/boost/multi_array/iterator.hpp|43 col 7| error: forming pointer to reference type 'const std::pair<double, unsigned int>&'
 csrc/lsh_cpp/lsh.cpp: In member function 'lsh_cpp::neighbour_iterator_t lsh_cpp::lsh_t::pimpl_t::query(const object_identifier_t&) const':
csrc/lsh_cpp/lsh.cpp|125 col 13| error: result of 'operator->()' yields non-pointer result

注意:这个类是 boost::multi_array 的一部分,(我已经写过了),我没有直接实例化它。我在我的实例化上面写了。该类由 boost::multi_array 以这种方式实例化:

 operator_arrow_proxy<reference>
 operator->() const
 {
     return operator_arrow_proxy<reference>(this->dereference());
 }

“参考”的使用让我认为参考是有意的。是否有理由提及无效的参考?我想记得自己做过几次,并以这种方式获取原始别名变量的地址....

4

1 回答 1

4

获取引用的地址不是问题,但它返回指向底层类型的指针,而不是指向引用的指针。无法创建指向引用的指针,它们也没有意义,因为引用不能被反弹。声明指向引用类型的指针是错误的。

因此,如果是引用类型,则返回类型T *将不起作用。T同样,mutable T如果 a 是引用类型,则声明 a 没有意义T,因为引用不能被反弹。所以operator_arrow_proxy显然写的是期望非参考。

如果 boost 用reference任何东西的成员实例化它,它总是一个引用类型,它看起来像一个错误。事实上,似乎被报告为错误 #6554

于 2013-10-16T08:05:49.623 回答