cpp: Add C++20 [[nodiscard]] attribute to operator new
C++20 standard added [[nodiscard]] attribute to operator new definitions. In case value returned by operator new is ignored, compiler emits a warning. The commit adds the attribute to operator new definitions. Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
This commit is contained in:
parent
7205f3e35b
commit
9f56e3d8a5
1 changed files with 16 additions and 10 deletions
|
@ -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<size_t>(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<size_t>(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<size_t>(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<size_t>(al), size);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue