Breyta

Deila með


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. Although this attribute was introduced in C++17, the compiler respects this attribute and generates warnings related to it when using /std:c++14 and later.

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 assigning the value to std::ignore or by casting it to void if discarding the value is intentional.
Assignment to std::ignore is preferred over casting to void in C++11 and higher, as it makes your intent clearer and will not trigger Warning C26457 if enabled in your code analysis settings.

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 four ways 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
    // If ignoring the [[nodiscard] attribute is unintentional, make use of the return value as intended:
    // For example:
    std::cout << "square_of(42) = " << square_of(42) << "\n"; // Ok
    // Or:
    int result = square_of(43); // Ok
    std::cout << "square_of(43) = " << result << "\n"; 

    // If ignoring the [[nodiscard]] attribute value is intentional, you have two options:
    // Preferrably, assign the return value to std::ignore:
    std::ignore = square_of(42); // Ok, C++11 and higher
    // Alternatively, you can cast the return value to void. 
    // The intent may be less clear to other developers.
    (void) square_of(42); // May produce warning C26457
    return 0;
}