Search code examples
c++language-lawyernew-operatorallocator

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?


Solution

  • 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.