C++ Logo

std-proposals

Advanced search

Re: [std-proposals] It seems impossible to use new on a class with a deleted operator delete

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Sat, 21 Sep 2024 18:50:31 +0000
If you delete the operator delete one would expect the operator new to also be deleted, just by symmetry.

But it doesn't seem incoherent, one does not have to use new or delete to write a valid program. Even though I am not familiar with the use case, it doesn't look like it necessarily leads to an ill formed application.



________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of Julian Waters via Std-Proposals <std-proposals_at_[hidden]>
Sent: Saturday, September 21, 2024 7:36:13 PM
To: std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Cc: Julian Waters <tanksherman27_at_[hidden]>
Subject: [std-proposals] It seems impossible to use new on a class with a deleted operator delete

Hi all,

I just noticed a strange thing in the language. If one deletes operator delete for a specific class, it becomes entirely impossible to create an object instance of that class with new expressions if exception handling is enabled and one is using a conforming compiler, since new expressions call operator delete to deallocate allocated memory if an exception is encountered. Nothing can be used to bypass this oddity in the language, from marking constructors and every method called in them as noexcept, to using LTO to allow the compiler to discern whether the constructor ever throws or not, for the simple reason that "the operator delete function is named by the new expression, regardless of whether the initialization can throw". This effectively means you cannot delete operator delete ever if you want to create an object instance of that class using new, unless you use language extensions, turn off exception handling entirely for C++, or otherwise use a non conforming compiler, which would pretty much mean you're not using C++, but one of many dialects of it. There are many usages of deleted operator delete, with one example off the top of my head being custom memory allocators in the HotSpot Java Virtual Machine (https://github.com/openjdk/jdk/blob/2461263aac35b25e2a48b6fc84da49e4b553dbc3/src/hotspot/share/memory/allocation.hpp#L391), so this seems like a strange limitation to me, is this intended or an oversight?

best regards,
Julian


Received on 2024-09-21 18:50:34