[prog] [C/C++] what to put in header files.

Kathryn Hogg kjh at flyballdogs.com
Mon Sep 23 17:07:13 EST 2002


> Oh, and no matter what you're ever told, including a .cpp file isn't
> considered very good practice.  :)
>

I'm assuming that he is talking about .C or .cc files (i hate .cpp because
that used to mean "c preprocessor" files) that contain nothing but
template implementations:

template <class _key, class _tp>
size_t hash_map<_key, _tp>::resize(size_t n_buckets)
{

   .....

}

Our code has to compile and run on linux, hpux, tru64, solaris, aix, and
windows using each vendors C++ compiler.  Some compilers (hp's) really
encourage you to separate your template code into a definition file (.h)
and implementation file (.C or .cc).  Other compilers like to see
everything at compile time.  So what I do is define various compiler
traits in a config.h file that we always include first in any .C file.

One of the traits I define is

#define NEEDS_TEMPLATE_INST 1

of the compiler likes you to have template implementations at compile time.

Then at the bottm of each template definition file (say hash_map.h):

#if NEEDS_TEMPATE_INST
#include <hash_map.C>
#endif

BTW, template instantion files should be guarded exactly like header files
(hash_map.C)

#ifndef KJH_HASH_MAP_C
#define KJH_HASH_MAP_C
....

#endif


Kathryn


_______________________________________





More information about the Programming mailing list