为什么cppreference说copy_n可以抛出bad_alloc?什么时候可以?

我已经通读了copy_n文档https://en.cppreference.com/w/cpp/algorithm/copy_n并且有一个有趣的Exceptions部分:

  • 如果算法分配内存失败,则抛出 std::bad_alloc 。

它在谈论什么分配?
当我们要复制N个字节时,我们首先自己分配一个缓冲区T* buffer = new T[SIZE],然后使用copy_n给定的内存分配时存在SIZE
什么情况copy_n

回答

本节讨论带有名为 的模板参数的重载ExecutionPolicy。该重载允许算法使用多个线程进行复制。为了促进这一点,实现可能需要分配一些资源,这可能会引发。

这些例外不适用于算法的串行版本。

  • @largest_prime_is_463035818 Yes, the actual copy operation could throw, but that's not `copy_n` that's throwing the exception. On the cppreference page, they are talking about the exceptions the algorithm itself could throw, and only the parallel version could fail to allocate as the serial version needs no allocation.

以上是为什么cppreference说copy_n可以抛出bad_alloc?什么时候可以?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>