Where Can It Be Used?

This lesson showcases some situations where the use of [[nodiscard]] is beneficial.

Attributes are a standardised way of annotating the code. They are optional, but they might help the compiler to optimize code, detect possible errors or just clearly express the programmer’s intentions.

Here are a few places where [[nodiscard]] can be potentially handy:

Errors

One crucial use case for [[nodiscard]] are error codes.

How many times have you forgotten to check a returned error code from a function? (Crucial if you don’t rely on exceptions).

Here’s some code:

Get hands-on with 1400+ tech skills courses.