Compiler warning (level 1) C4834

discarding return value of function with 'nodiscard' attribute

Remarks

Starting in the C++17 Standard, the [[nodiscard]] attribute specifies that a function's return value isn't intended to be discarded. If a caller discards the return value, the compiler generates warning C4834.

To resolve this warning, consider why your code doesn't use the return value. Your use of the function may not match its intent. You can circumvent the warning by using a cast to void.

This warning was introduced in Visual Studio 2017 version 15.3 as a level 3 warning. It was changed to a level 1 warning in Visual Studio 2017 version 15.7. Code that compiled without warnings in versions of the compiler before Visual Studio 2017 version 15.3 can now generate C4834. For information on how to disable warnings introduced in a particular compiler version or later, see Compiler warnings by compiler version.

To turn off the warning without code changes

You can turn off the warning for a specific line of code by using the warning pragma, #pragma warning(suppress : 4834). You can also turn off the warning within a file by using the warning pragma, #pragma warning(disable : 4834). You can turn off the warning globally in command-line builds by using the /wd4834 command-line option.

To turn off the warning for an entire project in the Visual Studio IDE:

  1. Open the Property Pages dialog for your project. For information on how to use the Property Pages dialog, see Property Pages.
  2. Select the Configuration Properties > C/C++ > Advanced page.
  3. Edit the Disable Specific Warnings property to add 4834. Choose OK to apply your changes.

Example

This sample generates C4834, and shows a way to fix it:

// C4834.cpp
// compile using: cl /EHsc /std:c++17
#include <iostream>

[[nodiscard]]
int square_of(int i) { return i * i; }

int main()
{
    square_of(42); // warning C4834: discarding return value of function with 'nodiscard' attribute
    // to fix, make use of the return value, as shown here:
    // std::cout << "square_of(42) = " << square_of(42) << "\n";
    return 0;
}