Consider the following C++ code snippet:
static [[nodiscard]] int GetSomething()
{
return 64;
}
If you invoke this function like this:
int main()
{
GetSomething();
}
the code compiles successfully with MSVC!
Even at warning level 4 (/W4, which I use on almost all my VC++ projects)!
Why am I upset? Because in the above code I do discard the return value of GetSomething(), and even with the [[nodiscard]] attribute applied to the function, and compiling at /W4, the MSVC C++ compiler does not complain. I do want the compiler to complain in this case! 🙂
Dear MSVC C++ compiler, speak up! Say something like: “Hey, you discarded the return value of GetSomething(), which is marked with the [[nodiscard]] attribute. Please fix your code.”
The interesting thing is that, if you change the position of the [[nodiscard]] attribute, putting it before “static”, you get the expected warning:
// Was:
// static [[nodiscard]] int GetSomething()
//
// Now put [[nodiscard]] at the beginning of the line,
// before "static":
//
[[nodiscard]] static int GetSomething()
{
return 64;
}
Now the C++ compiler complains when trying to compile the above code snippet inside main(), with: “warning C4834: discarding return value of function with [[nodiscard]] attribute”. Great!
The problem is that, a wrong behavior is being silently accepted, even at warning level 4! The MSVC compiler gave me a false sense of security!
I compiled the above code in C++20 mode at /W4 with Visual Studio 2022.
Interestingly, both Clang and GCC refuse to compile the code with [[nodiscard]] in the wrong position, while unfortunately MSVC does silently swallow that.
For example, Clang emits the following clear error message:
![Clang correctly emits an error when [[nodiscard]] is written in the wrong position.](https://giodicanio.com/wp-content/uploads/2026/10/cpp_nodiscard_clang_error.png?w=828)
On Windows, my favorite IDE for C++ (and some C#) programming is Visual Studio, and I use the Microsoft Visual C++ compiler (MSVC) to compile my C++ code. I hope that this silent sneaky compiler bug is fixed ASAP.

![C4834 warning message emitted by VC++ 2019 in its default C++14 mode, when the return value of a [[nodiscard]] function is discarded.](https://giodicanio.com/wp-content/uploads/2024/01/cpp_vs2019_default_cpp14_nodiscard_warning.png?w=974)