3 hours ago · Tech · hide · 0 comments

Consider the following C++ code snippet: static [[nodiscard]] int GetSomething() { return 64; } If you invoke this function e.g. inside main() like this: int main() { GetSomething(); } this 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 did discard the return value of GetSomething(), and even with the [[nodiscard]] attribute applied to the function, and compiling at /W4 (warning level 4), the MSVC C++ compiler did not complain. I did want the compiler to complain! Dear MSVC C++ compiler, speak up! Say something like: “Hey, you discarded the return value of GetSomething(), which was 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…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.