...

/

Third standard attribute

Third standard attribute

This section delves into the details of the third standard attribute called nodiscard.

We'll cover the following...

The third standard attribute we get in C++17 is:

[[nodiscard]] attribute

[[nodiscard]] can be applied on a function or a type declaration to mark the importance of the returned value:

Press + to interact
#include <iostream>
using namespace std;
[[nodiscard]] int Compute();
void Test()
{
Compute(); // Warning! return value of a
// nodiscard function is discarded
}

The above code should emit a warning when you compile it as you haven’t assigned the result to a variable.

What it means is that you can force users to handle errors. For example, what happens if you forget about using the return value from new or ...