Currently, allocation functions in the standard library take std::nothrow_t
by const reference like:
void* operator new ( std::size_t count, const std::nothrow_t& tag);
void* operator new[]( std::size_t count, const std::nothrow_t& tag);
Since std::nothrow_t
is simply a tag type for dispatching purposes, isn't it simpler and (possibly) more efficient to take it by value? For example:
void* operator new ( std::size_t count, std::nothrow_t tag);
void* operator new[]( std::size_t count, std::nothrow_t tag);
What is the rationale behind the const reference design?
答案 0 :(得分:5)
If the typical use of this version of new
were to call it as new(nothrow_t()) T
, you would have a good point. But that isn't the typical use, the typical use is new(nothrow) T
, where nothrow
is declared as extern const std::nothrow_t nothrow;
. Even though there is no actual data in a nothrow_t
type, it still takes up (at least) a byte, and in many ABIs that would mean that that (again, at least) one byte would need to be read from the nothrow
object in order to pass it to operator new
. Taking it by reference merely requires loading the object's address. So no, if the performance difference is measurable at all, I wouldn't expect the by-value version to be faster here.