0

is there a way in C++ to make it obvious that certain headers must be ordered exactly as shown, and reordering them will break the program?

For example:

// WARNING , THE TWO HEADERS BELOW MUST BE IN THIS ORDER EXACTLY.
    #include <winsock2.h>
    #include <windows.h>
// END WARNING.

#include <iphlpapi.h>
#include <stdio.h>
#include <stdint.h>

#pragma comment(lib, "Ws2_32.lib")

Is what I have right now, but I feel like C++ SHOULD have a feature to group headers together, something like:

#include_order <winsock2.h, windows.h>
#include <windows.h>
#include <winsock2.h>    
#include <iphlpapi.h>
#include <stdio.h>
#include <stdint.h>

#pragma comment(lib, "Ws2_32.lib") 

This way, no matter how people rearrange your code in the future, as long as the enforcement is first, the code won't break.

This is trivial enough to write a preprocessor for, but I was wondering if I can do this already without writing my own.

full code:

// WARNING , THE TWO HEADERS BELOW MUST BE IN THIS ORDER EXACTLY.
    #include <winsock2.h>
    #include <windows.h>
// END WARNING.

#include <iphlpapi.h>
#include <stdio.h>
#include <stdint.h>

#pragma comment(lib, "Ws2_32.lib")

int main()
{


    return 0;
}

standard way to deal with the issue: (WIN32_LEAN_AND_MEAN does seem like a misnomer)

/* this definition must precede any includes. */
#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <windows.h>

#include <iphlpapi.h>
#include <stdio.h>
#include <stdint.h>

#pragma comment(lib, "Ws2_32.lib")

int main()
{


    return 0;
}
Dmytro
  • 5,068
  • 4
  • 39
  • 50
  • 1
    Your code should not depend on the order in which headers are included. – juanchopanza Apr 09 '16 at 19:27
  • It should not, but when I rearrange those 2 headers listed above, i get over 100(116 to be exact) errors in my code. None of which say "oh, you should rearrange the two includes' order, duh". – Dmytro Apr 09 '16 at 19:27
  • 1
    Then fix the headers, or tell the people who are responsible for them to fix them. – juanchopanza Apr 09 '16 at 19:28
  • Asking me to call microsoft to fix their headers does not sound like a solution to the problem. Having a header order enforcer fixes this problem now without having to deal with microsoft. – Dmytro Apr 09 '16 at 19:29
  • There are other ways to solve the problem http://stackoverflow.com/questions/21399650/cannot-include-both-files-winsock2-windows-h. I'm not sure if a preprocessor order macro is a good idea. – perencia Apr 09 '16 at 19:31
  • Ah, so #define WIN32_LEAN_AND_MEAN does address this problem. It does not make it obvious to beginners that this is what it does, but it does do it. It really should be more explicit/better named though. Good to know that they did think of it though and do not force people to remember the order. – Dmytro Apr 09 '16 at 19:33
  • `WIN32_LEAN_AND_MEAN` doesn't do magic reordering, but excludes some of the less frequently used API of `"` (see [what-does-defining-win32-lean-and-mean-exclude-exactly](http://stackoverflow.com/questions/11040133/what-does-defining-win32-lean-and-mean-exclude-exactly) for more detail) which turn to be in conflict with the other header you use. – Jarod42 Apr 09 '16 at 19:46
  • 2
    This is trying to fix a flat tyre by replacing the wheel with a pizza. – Lightness Races in Orbit Apr 09 '16 at 19:48
  • When header A requires header B it should just `#include ` – stark Apr 09 '16 at 19:51

1 Answers1

1

The future proof way to fix this is if you are using visual studio:

Properties => C/C++ => Preprocessor => Definitions => WIN32_LEAN_AND_MEAN

It will define WIN32_LEAN_AND_MEAN globally for your whole project. If you are not using visual studio, find the setting in your IDE that will let you do the same thing.

You could also fix this by using a precompiled header header. If you define WIN32_LEAN_AND_MEAN there, you are safe because no header can be included before the precompiled header.

If someone modifies the precompiled header and wonders why the whole project doesn't compile anymore, then I'm pretty sure they will know where to look. Plus you can include a big comment there.

Gam
  • 1,254
  • 1
  • 9
  • 18
  • This eliminates the possibility of somebody moving WIN32_LEAN_AND_MEAN to the bottom of includes due to OCD, so it solves the specific case. Although they can still remove WIN32_LEAN_AND_MEAN from the settings because they don't think it does anything, but that's another issue. – Dmytro Apr 09 '16 at 20:19