Why are non-placement `new` and `delete` built into the language and not just regular functions?

Viewed 3062

Why were the non-placement new expression and the delete expression implemented as language built-in instead of regular functions?

If we have...

  • a way of requesting/giving back memory to the OS

  • a way of explicitly invoking a constructor (placement new)

  • a way of explicitly invoking a destructor (~T())

...why couldn't non-placement new and delete just be regular functions in the Standard Library? Example:

template <typename T, typename... Ts>
T* library_new(Ts&&... xs)
{
    auto* ptr = /* request enough memory for `T` from OS */;
    new (ptr) T(std::forward<Ts>(xs)...);
    return ptr;
}

template <typename T>
void library_delete(T* ptr)
{
    ptr->~T();
    /* reclaim memory for `T` from OS */
} 
3 Answers
Related