Why do distribution functions accept std :: nothrow_t by reference rather than by value?

Currently, the distribution functions in the standard library accept std::nothrow_t by reference const, for example:

 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 just a tag type for sending purposes, is it not easier and (perhaps) more efficient to take it by value? For instance:

 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 for const const construction?

+5
source share
1 answer

If the typical use of this version of new to be called as new(nothrow_t()) T , you would have a good point. But this is not typical use, typical use of new(nothrow) T , where nothrow declared as extern const std::nothrow_t nothrow; . Although there is no actual data in the nothrow_t type, it still occupies (at least) bytes, and in many ABIs, which would mean that this (again, at least) one byte should be read from nothrow to pass it operator new . Taking it by reference, you just need to download the address of the object. So no, if the performance difference is generally measurable, I would not expect the higher cost version to be faster here.

+5
source

Source: https://habr.com/ru/post/1233027/


All Articles