错误:形成指向引用类型的指针'const std :: pair <double,unsigned =“”int =“”>&amp;'....我无法理解这个错误</double,>

时间:2013-10-16 07:41:38

标签: pointers boost c++11

我在尝试使用时遇到了一些错误 - &gt;在迭代器类型中。当我挖掘定义迭代器的库时,在我看来,每次操作都是正确的并且没有理由发生错误。这是代码,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&lt; double,unsigned int&gt;&amp;'”。这些是内部的库证据。为了记录,这是我在我的代码中的内容:

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());
 }

使用“参考”让我认为该参考是有意的。是否有理由将地址作为参考而不起作用?我想要记住自己已经做了几次,然后以这种方式获取原始的,别名变量的地址....

1 个答案:

答案 0 :(得分:3)

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

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

如果boost使用reference任何成员实例化它,总是一个引用类型,它看起来像一个bug。实际上,似乎报告为bug #6554