我已将示例代码最小化以重现此错误:
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/operators.hpp>
// Evil
namespace Evil
{
using BadGirl = boost::multiprecision::cpp_int;
class BadBoy
: public boost::totally_ordered2<BadBoy, BadGirl>
{
public:
BadBoy() {}
};
}
// Civilians
class Base // Abstract class inherited from boost::equality_comparable to get != operator for free
: public boost::equality_comparable<Base>
{
friend bool operator == ( Base const & lhs, Base const & rhs );
public:
Base() {}
virtual void Whatever() const = 0;
};
// Implementation of equality operator
bool operator == ( Base const & /*lhs*/, Base const & /*rhs*/ )
{
return true;
}
class Derived
: public Base
{
public:
Derived() {}
void Whatever() const override {}
};
int main()
{
Derived * derived1 = new Derived();
Derived * derived2 = new Derived();
Base * base1 = derived1;
Base * base2 = derived2;
// Calling the operator from the base class
bool result = *base1 != *base2; // != operator is got for free from boost::equality_comparable.
return 0;
}
代码的Evil
部分会在编译期间导致失败,并抱怨cannot instantialize abstract class (base)
。但它只能与cl
重现。 G ++没有构建和运行它的问题。
知道为什么名称空间Evil
内绝对独立的类可能会导致类Base
和Derived
出错?到目前为止,我只发现boost::multiprecision::cpp_int
可以触发这个,但可能还有其他我不知道的情况。