Можно ли попросить компилятор предоставить реализацию по умолчанию оператора сравнения на равенство для таких типов? (Это может быть необходимо для некоторого общего кода, требующего сравнения объектов.)
Эта программа:
Код: Выделить всё
union U {
constexpr bool operator ==(const U&) const = default;
};
// error in MSVC
bool a = ( U{} == U{} );
// error in Clang
constexpr bool b = ( U{} == U{} );
- Компилятор Visual Studio неявно удаляет оператор ==:
Код: Выделить всё
error C2280: 'bool U::operator ==(const U &) const': attempting to reference a deleted function
note: see declaration of 'U::operator =='
note: 'bool U::operator ==(const U &) const': function was implicitly deleted because 'U' is a union-like class
error C2088: built-in operator '==' cannot be applied to an operand of type 'U'
- Clang определяет оператор, но не позволяет использовать его в константных выражениях:
Код: Выделить всё
note: pointer to temporary is not a constant expression
note: temporary created here
9 | constexpr bool b = ( U{} == U{} );
- GCC и EDG не видят никаких ошибок и принимают программу.
Онлайн-демо: https://gcc.godbolt.org/z/WdqGYG4r1
Подробнее здесь: https://stackoverflow.com/questions/797 ... mpty-union
Мобильная версия