Skip to content

[clang] Cannot create object with new operator when the delete operator of a class is explicitly deleted. #163049

@yanjiew1

Description

@yanjiew1

Hello Clang maintainers,

If the delete operator in a class is explicitly deleted, the class cannot be created on the heap.

The following code cannot be compiled with Clang, but it ca be compiled with GCC.

#include <cstddef>
#include <cstdlib>

class TestClass {
public:
    void* operator new(std::size_t count) {
        return malloc(count);
    }

  void  operator delete(void* p) = delete;
  void  operator delete [](void* p) = delete;
    
};

int main(void)
{
    TestClass *test = new TestClass();

    return 0;
}

Please see this Compiler Explorer:

The is the error shown in the compiler explorer.

<source>:17:23: error: attempt to use a deleted function
   17 |     TestClass *test = new TestClass();
      |                       ^
<source>:10:9: note: 'operator delete' has been explicitly marked deleted here
   10 |   void  operator delete(void* p) = delete;
      |         ^
1 error generated.
Compiler returned: 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:gccDoes the clang frontend diverge from gcc on this issueduplicateResolved as duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions