diff --git a/subsys/cpp/cpp_new.cpp b/subsys/cpp/cpp_new.cpp index 4a2c0044602..899ffc3bbfe 100644 --- a/subsys/cpp/cpp_new.cpp +++ b/subsys/cpp/cpp_new.cpp @@ -13,45 +13,51 @@ #define NOEXCEPT noexcept #endif /* __cplusplus */ -void* operator new(size_t size) +#if __cplusplus < 202002L +#define NODISCARD +#else +#define NODISCARD [[nodiscard]] +#endif /* __cplusplus */ + +NODISCARD void* operator new(size_t size) { return malloc(size); } -void* operator new[](size_t size) +NODISCARD void* operator new[](size_t size) { return malloc(size); } -void* operator new(std::size_t size, const std::nothrow_t& tag) NOEXCEPT +NODISCARD void* operator new(std::size_t size, const std::nothrow_t& tag) NOEXCEPT { return malloc(size); } -void* operator new[](std::size_t size, const std::nothrow_t& tag) NOEXCEPT +NODISCARD void* operator new[](std::size_t size, const std::nothrow_t& tag) NOEXCEPT { return malloc(size); } #if __cplusplus >= 201703L -void* operator new(size_t size, std::align_val_t al) +NODISCARD void* operator new(size_t size, std::align_val_t al) { return aligned_alloc(static_cast(al), size); } -void* operator new[](std::size_t size, std::align_val_t al) +NODISCARD void* operator new[](std::size_t size, std::align_val_t al) { return aligned_alloc(static_cast(al), size); } -void* operator new(std::size_t size, std::align_val_t al, - const std::nothrow_t&) NOEXCEPT +NODISCARD void* operator new(std::size_t size, std::align_val_t al, + const std::nothrow_t&) NOEXCEPT { return aligned_alloc(static_cast(al), size); } -void* operator new[](std::size_t size, std::align_val_t al, - const std::nothrow_t&) NOEXCEPT +NODISCARD void* operator new[](std::size_t size, std::align_val_t al, + const std::nothrow_t&) NOEXCEPT { return aligned_alloc(static_cast(al), size); }