错误:形成指针指向引用类型'const std::pair&'…我无法理解这个错误。

时间:2021-03-12 05:37:58

I'm getting some errors when trying to use -> in an iterator type. When I dig in the library defining the iterator, it seems to me that everyhing is allright and that there is no reason for the error. Here is the code, part of boost::multi_array:

在尝试使用迭代器类型中使用->时,会出现一些错误。当我在库中定义迭代器时,在我看来,每个人都是正确的,并且没有理由错误。这里是代码,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_;
};

which is instantiated with const std::pair<double, unsigned int>&; then the compiler complains about "forming pointer to reference type 'const std::pair<double, unsigned int>&'".Those are internal, library substantiations. For the record, here is what I have in my code:

该实例与const std::pair &;然后编译器会抱怨“形成指针指向引用类型的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;

and here is the usage that provokes the trouble:

这里有一个引起麻烦的用法:

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

Here's more error context:

这里有更多的错误上下文:

/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

NOTE: This class is part of boost::multi_array, (I already wrote that), and I'm not instantiating it directly. I wrote above my instantiation. The class is instantiated by boost::multi_array this way:

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

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

The use of "reference" makes me think that the reference is intended. Is there a reason for taking address to a reference to not work? I think to remember having done it myself a couple of times, and getting the address of the original, aliased variable that way....

使用“引用”使我认为引用是有意的。是否有理由将地址引用为不工作?我想记得曾经做过几次,和原来的地址,别名....变量方法

1 个解决方案

#1


3  

Taking address of a reference is not a problem, but it returns pointer to the underlying type, not pointer to reference. Pointers to reference can't be created nor would they make sense since references cannot be rebound. Declaring a pointer to reference type is an error.

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

The return type T * therefore won't work if T is a reference type. Similarly declaring a mutable T makes no sense if T is a reference type, because references cannot be rebound. So the operator_arrow_proxy is apparently written to expect a non-reference.

如果T是引用类型,则返回类型T *因此无效。同样地,如果T是引用类型,则声明一个可变的T是没有意义的,因为引用不能被反弹。因此,operator_arrow_proxy显然是为期望不引用而编写的。

If boost instantiates it with reference member of anything, which is always a reference type, it looks like a bug. Indeed, appears to be reported as bug #6554.

如果boost实例化它与任何东西的引用成员,它总是一个引用类型,它看起来就像一个bug。的确,似乎报告为bug #6554。

#1


3  

Taking address of a reference is not a problem, but it returns pointer to the underlying type, not pointer to reference. Pointers to reference can't be created nor would they make sense since references cannot be rebound. Declaring a pointer to reference type is an error.

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

The return type T * therefore won't work if T is a reference type. Similarly declaring a mutable T makes no sense if T is a reference type, because references cannot be rebound. So the operator_arrow_proxy is apparently written to expect a non-reference.

如果T是引用类型,则返回类型T *因此无效。同样地,如果T是引用类型,则声明一个可变的T是没有意义的,因为引用不能被反弹。因此,operator_arrow_proxy显然是为期望不引用而编写的。

If boost instantiates it with reference member of anything, which is always a reference type, it looks like a bug. Indeed, appears to be reported as bug #6554.

如果boost实例化它与任何东西的引用成员,它总是一个引用类型,它看起来就像一个bug。的确,似乎报告为bug #6554。