In the recently released Visual Studio 2022 version 17.14, std::expected
class template is marked with [[nodiscard]]
attribute.
As a result the program as follows:
#include <expected>
std::expected<void,int> f() {
return {};
}
int main() {
f();
}
started showing the warning:
warning C4834: discarding return value of function with [[nodiscard]] attribute
I know that ignoring std::expected
is not a good practice, but our codebase is large and it is hard to change all such places quickly.
I can silence the warning by writing (void)f()
here. But again, it requires to change every such calling place, which can be too many.
Is there a way to remove [[nodiscard]]
attribute from std::expected
in Microsoft STL, but keep the attribute for other functions and classes in the standard library?
I think it is not good idea to ignoring but if you want to suppress the warning only for specific parts of the code, you can use preprocessor directives like this.
#pragma warning(push)
#pragma warning(disable: 4834) // Disable specific warning for discarding [[nodiscard]]
f(); // No warning here
#pragma warning(pop) // Restore the warning state