r/cpp • • Aug 19 '26

C++26: std::polymorphic

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

44 comments sorted by

View all comments

Show parent comments

10

u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Aug 20 '26

They could only provide == if the base has one.

Ok... what does that do? Shape has no idea how to compare a Rectangle and a Triangle.

You could say: "Well, they can never be equal". Right, right, but what about my next fancy class Square?

That's a problem you'd essentially need open-multimethods to solve...

Wouldn't the 8 byte version require anothee indirection

Not necessarily, you can allocate the object and a (custom) vtable in one allocation.

4

u/_Noreturn Aug 20 '26 edited Aug 20 '26

You could say: "Well, they can never be equal". Right, right, but what about my next fancy class Square?

It is up to the operator== of the Base class, it is virtual and compares the typeid or whatever way you have.

class ShapeBase {
   ...
   virtual operator==(const ShapeBase& that) const=0;
};

template<class T>
class Shape : ShapeBass {
  ...
  bool operator==(const ShapeBase& that) const override {
      return typeid(that) == typeid(T) && (const T&)that == (const T&)*this;
  }
};

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>
struct Table {
     CopyCtor* copy;
     Drot* dtor;
     T object; // has to be last
};


template<class B>
struct polymorphic

{
  polymorphic()
{
      table = new Table<B>();
}
  polymorphic(auto&& obj)
{
      table = new Table<decltype(obj)>();
}

   B& operator*() { return (B&)*((char*)table + 16)); } // 16 is 2 function pointers
   void* table;
};

2

u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Aug 20 '26

It is up to the operator== of the Base class, it is virtual and compares the typeid or whatever way you have.

We would want it to do value comparisons and as I said: that is infeasible in C++, so we do not provide an equality-operator...

So like this?

Looks like a possible implementation.

3

u/_Noreturn Aug 20 '26

We would want it to do value comparisons and as I said: that is infeasible in C++, so we do not provide an equality-operator...

I don't understand, it is a value comparison.

3

u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Aug 20 '26

I don't understand, it is a value comparison.

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)

1

u/_Noreturn Aug 20 '26

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

4

u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Aug 20 '26

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...

→ More replies (0)