I found the following declaration on StackOverflow to replace the new
/ delete
operator. But i would like to understand this notation
How to properly replace global new & delete operators
void * operator new(decltype(sizeof(0)) n) noexcept(false)
Now let's split up: New obviusly needs to return a Pointer. And new
is used as an operator. So far so good. It is also clear to me the n
must represent the number of bytes to allocate.
The unclear part is the argument:
sizeof(0)
(on a 32bit machine) evaluates to sizeof(int) = 4
Then I got decltype(4 n)
???
What does this mean?
decltype(sizeof(0)) n
does not mean decltype(4 n)
. In
void * operator new(decltype(sizeof(0)) n) noexcept(false)
the decltype(sizeof(0)) n
part means the parameter is named n
and it has the type of what sizeof(0)
returns. You could replace this with
void * operator new(std::size_t n) noexcept(false)
since sizeof
is guaranteed to return a std::size_t
per [expr.sizeof]/6 but doing it the first way means you don't need to include <cstddef>