C++ Logo

std-proposals

Advanced search

[std-proposals] Polymorphic operator new and polymorphic values

From: Hans Åberg <haberg_1_at_[hidden]>
Date: Wed, 30 Apr 2025 11:05:56 +0200
I am using polymorphic (runtime, virtual) values val<A>; all classes A in the virtual hierarchy must have a polymorphic operator new. The polymorphic operator new is already present in C++, but one has to write it in by hand in each class; otherwise, the polymorphic copy and move operators will not work correctly.

I primarily want a way to automate this, with an addition to the language, say by adding “default” in the base class and then also the derived classes get it, or alternatively, the template system. It would also be nice to be able to use the keyword “new” instead of something else like “new_p“.

In my experience, the polymorphic values val<T> are better for objects that semantically are values than GC references, as the latter easily lead to programming errors. They also fit well with the C++ programming model, especially in view of move operators and C++17 copy elision.

So polymorphic values could be added to C++, in their own right.

The code for the class val<A> is in the file polymorphy.hh, and the operator new_p is defined in the file MLI.hh at lines 347–348:
https://git.savannah.gnu.org/cgit/metalogic-inference.git/tree/src

Received on 2025-04-30 09:06:15