cpp/memory/allocator/allocate:修订间差异
来自cppreference.com
无编辑摘要 |
小 P0600R1 nodiscard |
||
| 第8行: | 第8行: | ||
T* allocate( std::size_t n, const void * hint); | T* allocate( std::size_t n, const void * hint); | ||
}} | }} | ||
{{dcl | num=2 | | {{dcl | num=2 | =c++17 | = | ||
T* allocate( std::size_t n ); | T* allocate( std::size_t n ); | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
| 第28行: | 第30行: | ||
===注意=== | ===注意=== | ||
遣词“未指定何时及如何”令标准库容器可以[[cpp/language/new#分配|组合或优化掉堆分配]],即使这种优化对直接调用 {{tt|::operator new}} 被禁止。例如,这为 libc++ 所实现( [https://github.com/llvm-mirror/libcxx/blob/master/include/memory#L1766-L1772] 与 [https://github.com/llvm-mirror/libcxx/blob/master/include/new#L211-L217] ) | 遣词“未指定何时及如何”令标准库容器可以[[cpp/language/new#分配|组合或优化掉堆分配]],即使这种优化对直接调用 {{tt|::operator new}} 被禁止。例如,这为 libc++ 所实现( [https://github.com/llvm-mirror/libcxx/blob/master/include/memory#L1766-L1772] 与 [https://github.com/llvm-mirror/libcxx/blob/master/include/new#L211-L217] ) | ||
===参阅=== | ===参阅=== | ||
2017年11月14日 (二) 00:25的版本
| |
(1) | (C++17 前) |
| |
(1) | (C++17 起) (弃用) |
| (2) | (C++17 起) (C++20 前) |
|
| (C++20 起) | ||
以调用 ::operator new(std::size_t) 分配 n * sizeof(T) 字节的未初始化存储,但何时及如何调用此函数是未指定的。指针 hint 可用于提供引用的局部性:若实现支持,则 allocator 会试图分配尽可能接近 hint 的新内存块。
参数
| n | - | 要分配存储的对象数 |
| hint | - | 指向临近内存位置的指针 |
返回值
指向适当对齐并足以保有 T 类型的 n 个对象数组的内存块首字节的指针。
异常
若分配失败则抛出 std::bad_alloc 。
注意
遣词“未指定何时及如何”令标准库容器可以组合或优化掉堆分配,即使这种优化对直接调用 ::operator new 被禁止。例如,这为 libc++ 所实现( [1] 与 [2] )
参阅
[静态] |
用分配器分配未初始化的存储 ( std::allocator_traits<Alloc> 的公开静态成员函数)
|