为什么 rebind<U>::other 在 C++17 中被弃用并在 C++20 中被移除?

Why rebind<U>::other are deprecated in C++17 and removed in C++20?

我知道它仅在 std::allocator 中被弃用和删除。我可以在我自己的分配器上实现它。但为什么它被弃用了?

rebind 是一种笨拙的 C++11 之前的方法,它采用 T 的分配器类型并将其转换为 U 的分配器类型。我说“pre-C++11”是因为 C++11 为我们提供了一种更方便的方法:模板别名。

allocator_traits 模板有一个成员模板别名 rebind<U>,它为分配器计算 Allocator<U> 它是一个特征。如果可用,它可以使用分配器的 rebind 成员,否则,它只会产生 Allocator<U>.

allocator_traits 有效地定义了 std::allocator 过去提供的许多功能。这使得 std::allocator 的许多成员变得多余,因此它们已被弃用并删除,以支持基于 traits 的默认值。