为什么 `polymorphic_allocator` 采用 `memory_resource` 指针而不是引用?

Why does `polymorphic_allocator` take a `memory_resource` pointer and not a reference?

C++17 标准说:

[mem.poly.allocator.ctor]

polymorphic_allocator(memory_resource* r);
  • Requires: r is non-null.

  • Effects: Sets memory_­rsrc to r.

  • Throws: Nothing.

  • [ Note: This constructor provides an implicit conversion from memory_­resource*. — end note ]

如果 "requires" 子句提到 r 必须接受 memory_resource* 而不是 memory_resource& 有什么意义非空?

Bloomberg¹ 风格指南鼓励接受将被指针而不是引用改变的参数,以便调用方的 & 符号成为改变的视觉标记。但是,标准中没有这样的先例。

为什么r被当作指针而不是引用?


¹ pmr 在 Bloomberg 的大力参与下实现了标准化,因为该公司使用多态分配器模型。

N3916:

Note that the memory-resource library is designed so that the ShoppingList constructor accepts a pointer to a memory_resource rather than a reference to a memory_resource. It was noted that one common practice is to use references rather than pointers in situations where a null pointer is out of contract. However, there is a more compelling practice of avoiding constructors that take objects by reference and store their addresses. We also want to avoid passing non-const references, as that, too, is usually considered bad practice (except in overloaded operators).