How about the order of global vs local include files in C ++

Is there a recommended practice, which, for example, includes global inclusions, should be included for local ones. By global, I mean #include <iostream> and local #include "myhdr.h" . Is this some kind of preferred order and why?

+4
source share
1 answer

Yes, there are recommendations. Some of them:

  • Order . If you are in MyClass.cpp, first put "MyClass.h", then the C-headers, then the STL headers, and then your project headers.
  • The internal order . Use the alphabetical order in each of these categories.
  • Syntax Use #inlcude <> for C and STL and #include "" for your own headers.

They should look something like this:

 #include "MyClass.h" #include <time.h> #include <iostream> #include <vector> #include "MyFolder/MyAwesomeClass.h" #include "MyOtherFolder/MyOtherClass.h" 

For more recommendations on good coding style, you can take a look at the Google C ++ Style Guide . They give a good explanation of why you should do this in this section .

+12
source

Source: https://habr.com/ru/post/1441224/


All Articles