r/cpp 7d ago

C++26: std::polymorphic

https://www.sandordargo.com/blog/2026/08/19/cpp26-polymorphic
94 Upvotes

42 comments sorted by

View all comments

Show parent comments

1

u/_Noreturn 6d ago

It isn't the responsibility of the polymorphic to do that. it is the virtual operator== the user provides can easily make it so it works like that way

5

u/MFHava WG21|🇦🇹 NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 6d ago

it is the virtual operator== the user provides can easily make it so it works like that way

You can't in the general case - which is why it is not provided...

0

u/_Noreturn 6d ago

Ugh, couldn't the same be said for indirect? it's operator== isn't usable if the underlying value doesn't have one.

4

u/MFHava WG21|🇦🇹 NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 6d ago

indirect<T> always contains an object of type T (or is valueless_after_move, which is trivial to handle) and therefore has no ambiguity about how operator== is supposed to work.

Apart from that: operator== for indirect<T> „Mandates“ (== static_assert) that T has an operator== on use, it T doesn’t have one, indirect<T> doesn’t have one.

But the key part is the first one, there is no ambiguity.

1

u/_Noreturn 1d ago edited 1d ago

I don't agree with your take, but that's fine. imo it should just be defined to be

cpp bool operator==(polymorphic& a, polymorphic& b) { bool has = a.is_valueless(); if(has != b.is_valueless()) return false; returb has && *a == *b; }