(MSVC 2017 /WALL & /WX) Including Iostream Produces 800 Warnings

Viewed 165

Reviewing material for an optimized C++ course next quarter. The professor for this course is enforcing /WALL and /WX for our project properties. The problem I'm having is that including the Iostream library produces over 800 warnings. Here's the code I'm attempting to run:

#include "pch.h"
#include <iostream>

int main() {
  std::cout << "Hello World";
  return(0);
}

A few of the warnings that I'm receiving includes:

C4514 'abs': unreferenced inline function has been removed
C4774 'sprintf_s': format string in argument 3 is not a string literal
C4820 'std::basic_ios ...': '7' bytes of padding added after...

Before asking Stack I emailed the Prof to ask about the warnings and was told:

You should be including iostream

If you get 100 warnings you included a header that's not needed

Is there something I'm missing? I know I wouldn't be able to edit source files for iostream as that's not portable coding. I looked around to see if I could explicitly include functions such as cout, cin, etc. Yet, I don't feel like this is the correct solution.

Edit:

A user requested an example of a more explicit warning message in case there was something missing in there. Here are a few:

C415 'abs': referenced inline function has been removed (Project: Hello World) (File: stdlib.h)
C4710 'int sprintf_s(char *const....: function not inlined. (Project: Hello World) (File: stdio.h)

The professor is using GCC through Visual Studio and our settings are pulled from a repository as premade projects.

1 Answers

Your professor is, quite simply, wrong.

This has nothing to do with "including a header that's not needed" (why would that generate warnings?), but with using /WALL, which reveals some flaws in the stdlib implementation there!

This switch is not recommended; quoting James McNellis who gets it bang-on under the above referenced question:

/Wall enables many warnings that, while potentially useful sometimes, are not useful most of the time. /Wall in Visual C++ does not mean the same thing as -Wall on g++ (really, g++ "has /Wall wrong," since it doesn't actually enable all warnings). In any case, in Visual C++, all of the commonly important and useful warnings are enabled by /W4.

I would use /W4 in Visual Studio (and -Wall -Wextra in GCC).

Obviously I can't help you to persuade your professor of this, other than to suggest saying something along the lines of "I asked on Stack Overflow and found out that this is due to /Wall being too strict and generating warnings on Visual Studio's own headers. They suggest we use /W4 instead. . What do you think?"

It is true that you need to #include <iostream>, and it is true that you should never modify the provided standard headers. Also, don't forget to stream a '\n' to end your output line!

Related