← Back to team overview

unity-dev team mailing list archive

Re: [Ayatana-dev] [C++][Review Guidelines] Header files and includes

 

On Thu, 23 Jun 2011 02:56:54 Jay Taoko wrote:
> >   * include system files that you use
> >   
> >     - if you use std::map, include<map>  and don't rely on transitive
> >     inclusion
> > 
> > (i.e. if you are using "A" and "A" uses<map>, you should still
> > include<map> as you are using it.  The include guards make the compiler
> > handle this correctly.)
> > 
> >     - the reason for this is that "A" may stop using<map>  and you don't
> >     want
> > 
> > that to break your code.
> 
> I have added many of the C++ standard includes in NuxCore/NuxCore.h. And
> I expected that all cpp files in Nux will have a first included files
> that will have NuxCore.h. For instance, Nux/Nux.h includes
> NuxCore/NuxCore.h and I expect all cpp files in Nux to have Nux.h as the
> first included file.
> Is that wrong?

Short answer, yes.

Sure, it works, but you are enforcing dependencies where they may not exist.

What we should aim for is small (FSVO small) files, both headers and source 
files.  A lot of the standard C++ headers are quite large and bring in a lot of 
template classes that the compiler needs to parse that are often not used in 
the source file.

NuxCore/NuxCore.h currently has:

#include <cassert>  // (assert.h)
#include <cctype>   // (ctype.h)
#include <cerrno>   // (errno.h)
#include <cfloat>   // (float.h)
#include <ciso646>  // (iso646.h)
#include <climits>  // (limits.h)
#include <clocale>  // (locale.h)
#include <cmath>    // (math.h)
#include <cstdarg>  // (stdarg.h)
#include <cstddef>  // (stddef.h)
#include <cstdio>   // (stdio.h)
#include <cstdlib>  // (stdlib.h)
#include <cstring>  // (string.h)
#include <ctime>    // (time.h)
#include <cwchar>   // (wchar.h)
#include <cwctype>  // (wctype.h)
#include <exception>
#include <stdexcept>

#include <iostream>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <iomanip>
#include <map>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <new>
#include <set>

Now that is a huge amount to bring in if you have a very simple source file.

We need to aim for minimilism.  Only include what you use, but include 
*everything* that you use.  Every header should be "stand alone".  By that I 
mean that you shouldn't have to include anything else before bringing in that 
header in order for it to compile.

I posit that cleaning up the includes in nux and only including what is needed 
will reduce the compile time by 50%.

Then there are other minor things, like having <iostream> in a header file - 
should only have <iosfwd>.

Tim

Attachment: signature.asc
Description: This is a digitally signed message part.


References