Search code examples
c++shared-ptrnew-operator

C++ deleted new operator but can create shared_ptr


I tried looking for an answer, but couldn't find anything helpful to understand what happens behind the scenes.

Assuming that we have the following:

class Base
{
public:
    virtual int request() = 0; 
    void* operator new(long unsigned int size) = delete;
};


class Derived final : public Base
{
public:
    int request() override;
};

int Derived::request()
{
    return 2;
}

On the base class I have deleted the new operator because I don't want to be able to create pointers. I know it might not make any sense, it is just an experiment.

When I call g++ gives me a compilation error to tell me that the new operator is deleted. Exactly what I expected.

int main()
{
    auto d =  std::make_unique<Derived>();
    int value = d->request();
    std::cout << "Request value is " << value << "." << std::endl;

    return 0;
}

But if I create a shared_ptr the code compiles and runs fine.

    auto d =  std::make_shared<Derived>();

Why is this code valid when creating shared pointers. The operator is not being used in this case?


Solution

  • std::make_shared use global placement new version of the operator new, by std::allocator::construct

    It is necessary to store object and atomic reference counter in the same block of memory, so that shared_ptr template works almost equal to intrusive smart pointer.

    You can not prevent std::make_shared to construct a smart pointer.