是否可以将 std::nothrow 参数用于放置新操作?

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

我有一个用例,我想使用 placement new 但也想确保 new 操作不会抛出。

我想我想要像下面这样的东西(注意这不能编译)但我不确定具体怎么做。

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

无需在 placement new 表达式中使用 std::nothrow。与 new/new[] 不同,展示位置 new/new[] 已被定义为 noexcept.

[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;

如果您查看 [new.delete.single] and [new.delete.array],您会发现展示位置版本将调用相应的 std::nothrow 版本的 new/new[]