简体   繁体   English

是否可以使用std :: nothrow参数进行放置新操作?

[英]Is it possible to use the std::nothrow argument for a placement new operation?

I have a use case where I want to use placement new but also want to ensure that the new operation won't throw. 我有一个用例,我想使用placement new,但也希望确保新操作不会抛出。

I figure I'd want something like the below (note this doesn't compile) but I wasn't sure how exactly to do it. 我想我想要像下面这样的东西(注意这不会编译),但我不确定如何做到这一点。

char buf1[100];                                                                                                           
Foo* foo = new (std::nothrow) (buf1) Foo(100);

There is no need to use std::nothrow in a placement new expression. 无需在放置新表达式中使用std::nothrow unlike new / new[] , placement new / new[] are already defined as being noexcept . new / new[] ,placement new / new[]已被定义为noexcept

[new.delete.placement] [new.delete.placement]

 [[nodiscard]] void* operator new(std::size_t size, void* ptr) noexcept; [[nodiscard]] void* operator new[](std::size_t size, void* ptr) noexcept; 

If you look in [new.delete.single] and [new.delete.array] you'll see that the placement version will call the corresponding std::nothrow versions of new / new[] . 如果查看[new.delete.single][new.delete.array],您会看到展示位置版本将调用new / new[]的相应std::nothrow版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM