Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why std::allocator<T>::allocate calls ::operator new?

The documentation for the std::allocator<T>::allocate member function says in ([allocator.members]) that:

Remarks: The storage for the array is obtained by calling ​::​operator new ([new.delete]), but it is unspecified when or how often this function is called. This function starts the lifetime of the array object, but not that of any of the array elements.

I wonder why it says ::operator new and not just operator new? Does the double colon make any difference? Which other operator new could be called here, if that double colon was omitted?

like image 707
Daniel Langr Avatar asked Jan 27 '26 23:01

Daniel Langr


1 Answers

Prior to LWG2818, [contents]p3 read:

Whenever a name x defined in the standard library is mentioned, the name x is assumed to be fully qualified as ::std::x, unless explicitly described otherwise. For example, if the Effects: element for library function F is described as calling library function G, the function ::std::G is meant.

So writing operator new in the specification would mean ::std::operator new, which wouldn't make sense. ::operator new correctly refers to operator new in the global namespace.

In an implementation of the standard library, there would be no difference between writing operator new and ::operator new since std::allocator<T> wouldn't define a member operator new and ADL has no effect since there is only one namespace a free operator new could be defined in.

like image 135
Artyer Avatar answered Jan 29 '26 11:01

Artyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!