Summary
In this episode, Jason Turner explores the [[nodiscard]] attribute introduced in C++17 and enhanced in C++20. He demonstrates how it can help prevent bugs by forcing developers to handle return values that shouldn't be ignored. Using the example of a vector's empty() method, he shows how ignoring its return value is a common mistake (users often confuse it with clear()). C++20 extends the attribute to allow custom messages, making warnings more informative, and [[nodiscard]] can be applied to both functions and entire types.
Related C++ Standard Sections
This episode covers topics found in these sections of the C++ standard:
-
[underflow.error]45% match -
[overflow.error]45% match -
[optional.bad.access]44% match