CPP-6162 S1265 Require sized delete since C++14 and definitions in the same file
* CPP-6162 S1265 Require sized delete since C++14 and definitions in the same file * Remove requirement to be in the same file --------- Co-authored-by: Mostafa Mohammed <mostafa.mohammed@sonarsource.com>
This commit is contained in:
parent
1060d8a1b4
commit
99e38a9163
@ -14,7 +14,7 @@ Up to this point, we mentioned `operator new` and `operator delete`, but it is a
|
||||
|
||||
For instance, if `++void * operator new[]( std::size_t count, std::align_val_t al );++` is defined (for arrays, with extra alignment), then `++void operator delete[]( void* ptr, std::align_val_t al ) noexcept;++` should be defined too.
|
||||
|
||||
Additionally, since {cpp}17, it is possible to define a version of the delete operator with an additional size argument, alongside the unsized version of `operator delete`. When overloading these operators in a class, defining both a sized and an unsized version of operator delete is useless, since the unsized version will always be preferred. However, for free replacement, it is necessary to specify both versions since the language does not specify which version will be called.
|
||||
Additionally, it is possible to define a version of the delete operator with an additional size argument, alongside the unsized version of `operator delete`. When overloading these operators in a class, defining both a sized and an unsized version of operator delete is useless, since the unsized version will always be preferred. However, for free replacement (introduced in {cpp}14), it is necessary to specify both versions since the language does not specify which version will be called.
|
||||
|
||||
=== What is the potential impact?
|
||||
|
||||
@ -24,7 +24,7 @@ Deallocating memory that was not allocated with the corresponding strategy resul
|
||||
|
||||
== How to fix it
|
||||
|
||||
Each overload of the `operator new` should have a matching overload `operator delete` and vice versa. Since {cpp}17, within a class, define only a sized or an unsized version of `operator delete`, but as a free function, define both.
|
||||
Each overload of the `operator new` should have a matching overload `operator delete` and vice versa. Within a class, define only a sized or an unsized version of `operator delete`, but as a free function (availalble since {cpp}14), define both.
|
||||
|
||||
=== Example with overloaded operators in a class
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user