c++ - C++ allocator::allocate 是否应该抛出?

标签 c++ std allocator

Allocator conceptstd::allocator_traits没有说明 allocate 是否会抛出。

所以当我使用分配器编写容器时,如何知道是检查返回类型还是使用 catch

最佳答案

您引用的页面的Requirements 部分中的表格提供了足够的信息,说明何时可以抛出异常以及何时不得抛出异常。下面是分配器可能抛出异常的引用。

a.allocate(n) allocates storage suitable for n objects of type T, but does not construct them. May throw exceptions.

那里没有描述抛出什么类型的异常,可能取决于实现。在STL中一般是std::bad_alloc

关于c++ - C++ allocator::allocate 是否应该抛出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50312496/

相关文章:

c++ - 我找不到 qt-creator 应该制作的 .exe 文件

c++ - 使用 std::tuple 从 map 中输出

c++ - 使用默认容器,但在 priority_queue 中使用自定义比较器

c++ - 为什么 C++ 迭代器需要返回引用?

c++ - boost/STD/AKKA : How do I do actor programming in C++?

c++ - 为 boost::deque 保留 block 分配器?

c++ - 使用指针作为函数参数

parameters - Rust 标准库闭包参数 : run time or compile time?

c++ - 在具有预分配 block 的自定义 STL 分配器中重新绑定(bind)

c++ - 使用自定义分配器计算内存使用情况