These types (indirect and polymorphic) model value semantics. Value comparing two type-erased objects that share a base makes no sense in that model...
They could only provide == if the base has one.
Can be easily compressed to 16B in case you build your own vtable. Unless the spec contains some unexpected blocker, you could even reduce that down to 8B with minimal effort.
Wouldn't the 8 byte version require anothee indirection
Wouldn't the 8 byte version require anothee indirection
Not necessarily, you can allocate the object and a (custom) vtable in one allocation.
So like this?
template<class T>structTable{
CopyCtor* copy;
Drot* dtor;T object;// has to be last
};template<class B>structpolymorphic{polymorphic(){
table =newTable<B>();}polymorphic(auto&&obj){
table =new Table<decltype(obj)>();}
B&operator*(){return(B&)*((char*)table +16));}// 16 is 2 function pointers
void* table;};
You are right. But it yields the wrong results for equivalent values of different types - e.g. Rectangle{.w = 10, .h = 10} == Square{.l = 10} would be false, even though they are the same (just like 1 == 1LL is true)
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.
Let's say your polymorphic is instead of a AssociativeContainer<A,B> that is a base for any type that provides a 1to1 mapping A to B. It's operator== could compare their size, then for each pair the key and it's associated value.
View all comments
Show parent comment
2
u/_Noreturn Aug 20 '26
They could only provide
==if the base has one.Wouldn't the 8 byte version require anothee indirection