The C++17 Standard says:
polymorphic_allocator(memory_resource* r);
Requires:
r
is non-null.Effects: Sets
memory_rsrc
tor
.Throws: Nothing.
[ Note: This constructor provides an implicit conversion from
memory_resource*
. — end note ]
What's the point of accepting a memory_resource*
instead of a memory_resource&
if the "requires" clause mentions that r
must be non-null?
The Bloomberg¹ style guide encourages accepting arguments that are going to be mutated by pointer instead of reference so that the ampersand on the caller side becomes a visual marker for mutation. However, there is no such precedent in the Standard.
What is the reason that r
is taken as a pointer and not as a reference?
¹ pmr
was standardized with heavy Bloomberg participation, as the company uses a polymorphic allocator model.
[io]stream
s that takestreambuf
pointer. These are the only other entities instd
I can think of that are a similar kind of non-owning interface adapter. – DeconsecrateEffects: Sets memory_rsrc to r.
might be the relevant clause. By taking the argument by-pointer rather than by-reference, the API encourages the programmer to think more carefully about object-lifetime issues regarding the passed-in object. In particular, when calling a function/method that takes an argument by-reference, programmers who didn't read the documentation carefully (read: most of us ;) ) would likely not expect that function to retain a pointer to the passed-in object for later use after the call has returned. – Croy