What is the exact meaning of the phrase "as if" in the standard and how does it work when a user can modify individual parts of the behavior.
The question is in regards to the C++ standard when talking about the nothrow version of operator new
. 18.4.1.1/7 reads (my emphasis):
This nothrow version of operator new returns a pointer obtained as if acquired from the ordinary version.
My understanding is that "as if" does not require a specific implementation as long as the behavior is appropriate. So if operator new
was implemented like this (I know this is not a compliant implementation as there is no loop or use of the new_handler; but I'm shortening that to focus on my issue):
// NOTE - not fully compliant - for illustration purposes only.
void *operator new(std::size_t s)
{
void *p = malloc(s);
if (p == 0)
throw std::bad_alloc();
return p;
}
Then it would be legal to write the nothrow version like this:
// NOTE - not fully compliant - for illustration purposes only.
void *operator new(std::size_t s, const std::nothrow_t &nt)
{
return malloc(s);
}
But let's say a program replaces operator new
to use some other allocator. Does "as if" mean the compiler has to automatically change the behavior of the nothrow version to use this other allocator? Is the developer required to replace both the plain and nothrow versions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…