gpt4 book ai didi

c++ - 如果指定 value_types 不同的分配器会怎样?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:09 25 4
gpt4 key购买 nike

假设我声明 std::vector<std::string, std::allocator<char>> .可能发生的更糟情况是什么?我运行了它,似乎没有遇到任何内存错误。

最佳答案

它会导致未定义的行为。对于像 vector 这样的分配器感知容器,标准在表 99 中指定:

Requires: allocator_type::value_type is the same as X::value_type.

(请注意,同一行还指定 allocator_type 必须容器特化的模板参数的 typedef)

根据 [res.on.functions]/2:

In particular, the effects are undefined in the following cases:
[…]
— for types used as template arguments when instantiating a template component, if the operations on the type do not implement the semantics of the applicable Requirements subclause (17.6.3.5, 23.2, 24.2, 26.2).

关于c++ - 如果指定 value_types 不同的分配器会怎样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526046/

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