Quantcast
Channel: Comparing polymorphic types in c++20 - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Comparing polymorphic types in c++20

$
0
0

I have code that is somewhere between c++17 and c++20. Specifically, we have c++20 enabled on GCC-9 and clang-9, where it is only partially implemented.

In code we have quite big hierarchy of polymorphic types like this:

struct Identifier {    virtual bool operator==(const Identifier&other) const = 0;};struct UserIdentifier : public Identifier {    int userId =0;    bool operator==(const Identifier&other) const override {        const UserIdentifier *otherUser = dynamic_cast<const UserIdentifier*>(&other);        return otherUser && otherUser->userId == userId;    }};struct MachineIdentifier : public Identifier {    int machineId =0;    bool operator==(const Identifier&other) const override {        const MachineIdentifier *otherMachine = dynamic_cast<const MachineIdentifier*>(&other);        return otherMachine && otherMachine->machineId == machineId;    }};int main() {    UserIdentifier user;    MachineIdentifier machine;    return user==machine? 1: 0;}

https://godbolt.org/z/er4fsK

We are now migrating to GCC-10 and clang-10, but because of reasons we still need to work on versions 9 (well, at least clang-9 as this is what android NDK currently has).

The above code stops compiling because new rules about comparison operators are implemented. Reversible operator== causes ambiguities. I can't use a spaceship operator because it is not implemented in versions 9. But I omitted this from the example - I assume that whatever works with == will work with other operators.

So:What is the recommended approach to implementing comparison operators in c++20 with polymorphic types?


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images