提升错误:"运算符=="的重载不明确

boost error: ambiguous overload for ‘operator==’

本文关键字:quot 重载 不明确 运算符 错误      更新时间:2023-10-16

有人在boost中看到过这个错误吗?

我正在 CentOS 7 上使用 GCC 1.62 使用 boost 7.1 编译我的项目,我在下面遇到编译错误

是加速错误还是其他什么?

谢谢

boost/boost-1.62.0/include/boost/random/detail/polynomial.hpp: In member function ‘void boost::random::detail::polynomial::normalize()’:
boost/boost-1.62.0/include/boost/random/detail/polynomial.hpp:352:45: error: ambiguous overload for ‘operator==’ (operand types are ‘boost::random::detail::polynomial::reference’ and ‘int’)
while(size() && (*this)[size() - 1] == 0)
~~~~~~~~~~~~~~~~~~~~^~~~
boost/boost-1.62.0/include/boost/random/detail/polynomial.hpp:352:45: note: candidate: operator==(int, int) <built-in>
xxx.h:393:17: note: candidate: bool operator==(const bool&, const CPolyVal&)
STR_INLINE bool operator==(const TYPE& val, const CPolyVal& cpv)
^
xxx.h:400:1: note: in expansion of macro ‘IMPLEMENT_RHS_COMPARE_TO’
IMPLEMENT_RHS_COMPARE_TO(bool)
^~~~~~~~~~~~~~~~~~~~~~~~
xxx.h:393:17: note: candidate: bool operator==(const char&, const CPolyVal&)
STR_INLINE bool operator==(const TYPE& val, const CPolyVal& cpv)
^
xxx.h:401:1: note: in expansion of macro ‘IMPLEMENT_RHS_COMPARE_TO’
IMPLEMENT_RHS_COMPARE_TO(char)
^~~~~~~~~~~~~~~~~~~~~~~~

更多像这样。

更新了错误日志。

在我们的代码库中的某个地方。

#define IMPLEMENT_RHS_COMPARE_TO(TYPE)
STR_INLINE bool operator==(const TYPE& val, const CPolyVal& cpv)
{
TYPE cpv_equiv_val;
return cpv.ConvertTo(cpv_equiv_val) == val;
}
// end macro
IMPLEMENT_RHS_COMPARE_TO(bool)
IMPLEMENT_RHS_COMPARE_TO(char)
IMPLEMENT_RHS_COMPARE_TO(Int8)
IMPLEMENT_RHS_COMPARE_TO(Int16)
IMPLEMENT_RHS_COMPARE_TO(Int32)
IMPLEMENT_RHS_COMPARE_TO(Int64)
IMPLEMENT_RHS_COMPARE_TO(UInt8)
IMPLEMENT_RHS_COMPARE_TO(UInt16)
IMPLEMENT_RHS_COMPARE_TO(UInt32)
IMPLEMENT_RHS_COMPARE_TO(UInt64)
IMPLEMENT_RHS_COMPARE_TO(Float32)
IMPLEMENT_RHS_COMPARE_TO(Float64)
IMPLEMENT_RHS_COMPARE_TO(std::string)

从您在评论中发布的内容来看,您以某种方式设法声明了一些侵入性和过于宽泛的重载==运算符(模板?在这种情况下,Boosts 希望左侧的boost::random::detail::polynomial::reference被用户定义的转换运算符隐式转换为bool,而右侧的0应解释为false。因此,目的是使用内置的intint比较以比较两个bool值。

此时,您的运算符==声明显然对 Boost 代码可见,并产生歧义。 例如,发生了这样的事情

// Very loose and broad template comparison operator gets declared above
// It can compare anything to `bool`
template <typename T> bool operator ==(T lhs, bool rhs)
{
return false;
}
// Unsuspecting code begins here
// It assumes that `S` vs. `int` comparisons will be interpreted as
// built-in `bool` vs. `bool` comparisons
struct S
{
operator bool() const { return false; }
void normalize()
{
S s;
s == 0; // Error: ambiguous comparison
}
};