Are there conditions under which using "__attribute __ ((warn_unused_result))" will not work?

I tried to find the reason why this is not working in my code - I think this should work. Here is an excerpt from the header file:

#define WARN_UNUSED __attribute__((warn_unused_result)) class Trans { Vector GetTranslation() const WARN_UNUSED { return t; } }; 

So my question is: why am I not getting a warning when I compile the code with something like:

 Gt.GetTranslation(); 

?

Thanks for the help.

+6
source share
1 answer

The purpose of this attribute is intended (but not exclusively) for pointers to dynamically allocated data.

It gives a compile-time guarantee that the calling code will store a pointer in a variable (it can also be a parameter for a function, but I'm not sure), thereby delegating the responsibility of freeing / freeing / deleting the object it points to.

This is to prevent memory leak and / or other aspects of life time management.

for example, if you call malloc (...) without saving the pointer, you cannot release it later. (malloc must have this attribute)

If you use it for a function, return an object, which does not make sense, because the returned object is stored in a temporary state and can be copied to a non-temporary variable (can be optimized) and will always be destroyed (because it will be.

By the way, this is not particularly useful for returned links (if you do not know the code and do not require some kind of release mechanism), since the link object does not collapse when leaving the scope.

+2
source

Source: https://habr.com/ru/post/894235/


All Articles