gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:08:48 24 4
gpt4 key购买 nike

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 ]

接受 memory_resource* 有什么意义?而不是 memory_resource&如果 “需要” 子句提到 r必须是非空的?

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

是什么原因r被当作​​指针而不是引用?


¹ pmr由于该公司使用多态分配器模型,因此在彭博社的大力参与下实现了标准化。

最佳答案

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

关于c++ - 为什么 `polymorphic_allocator` 采用 `memory_resource` 指针而不是引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56013463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com