nodiscard attribute
void *allocatingFunc(...) { write(file); return malloc(...); }
What happens if we have this in our library and a unexperienced programmer starts calling the function in a loop? Consider that the function may not be as obvious as this.
Maybe the programmer hasn’t readed the specification and just wanted to call the function to perform the write to file functionality.
while(true) { //compute allocatingFunc(...); //saving my values! //compute }
Now you can do the following:
[[nodiscard]] void *allocatingFunc(...) { write(file); return malloc(...); }
This way you can notify the programer if the returned pointer is getting lost (and not being able to reclaim it in long running programs).
Also in C++20 you can do the following:
[[nodiscard("This function returns a pointer to allocated memory!")]] void *allocatingFunc(...) { write(file); return malloc(...); }
Now we can be more explicit by throwing a custom warning message.