|
|
|
|
|
by ComputerGuru
1117 days ago
|
|
Because a function can be assumed to work one or two ways and nodiscard can prevent you from assuming the wrong thing. Imagine a fake string::concat(other) that does what it says on the tin. You can assume it appends to s but what if it allocates a new str instead? You label it with nodiscard because there is literally zero purpose in ever calling this method but ignoring the result - if you ignore it, it’s as if it was never called (since the existing variables were not modified) except you paid the price with the allocations. So either use the result or drop the call. Using it to enforce that every return value is handled is stupid as it can lead to error blindness and you’ll ignore it when you actually need it. |
|