pointers - 错误 : forming pointer to reference type 'const std::pair<double, unsigned int>&' . ...我无法理解这个错误

标签 pointers boost c++11

尝试在迭代器类型中使用 -> 时出现一些错误。当我深入研究定义迭代器的库时,在我看来,一切都很好,没有错误的原因。这是代码,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/

相关文章:

c++ - map 和 unordered_map 包含指向 double 的指针?

c++ - 在函数中填充指针数组

c++ - boost::interprocess 互斥与 boost 线程互斥

c - 如何在 void* 中存储 int 指针?

c - 返回指针数组

c++ - 如何在 boost::dynamic_bitset<> 中查找第一次和最后一次出现

c++ - boost::archive::text_oarchive 的简单扩展

c++ - C++ lambda 不能正确选择重载函数吗?

c++ - 根据值的概率从 Map 中获取随机键

c++ - 检查类型实例是否可以流式传输