尝试在迭代器类型中使用 -> 时出现一些错误。当我深入研究定义迭代器的库时,在我看来,一切都很好,没有错误的原因。这是代码,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
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());
}
“引用”的使用让我觉得引用是有意的。是否有理由将地址引用不起作用?我想记得自己做过几次,并以这种方式获取原始别名变量的地址....
最佳答案
获取引用的地址不是问题,但它会返回指向基础类型的指针,而不是指向引用的指针。不能创建指向引用的指针,它们也没有意义,因为引用不能被反弹。声明指向引用类型的指针是错误的。
如果 T
是引用类型,则返回类型 T *
将不起作用。类似地,如果 T
是引用类型,则声明一个 mutable T
是没有意义的,因为不能重新绑定(bind)引用。所以 operator_arrow_proxy
显然是为了预期非引用而编写的。
如果 boost 使用任何东西的 reference
成员实例化它,总是 一个引用类型,它看起来像一个错误。事实上,似乎报告为 bug #6554 .
关于pointers - 错误 : forming pointer to reference type 'const std::pair<double, unsigned int>&' . ...我无法理解这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19397737/