Exceptions
Team LiB
Previous Section Next Section

Exceptions

The following entities with external linkage can go in header files:

  • Inline functions: These have external linkage, but the linker is guaranteed not to reject multiple copies. Other than that, they behave exactly like regular functions. In particular, the address of an inline function is guaranteed to be unique throughout a program.

  • Function templates: Similar to inline functions, instantiations behave just like regular functions except that duplicate copies are acceptable (and had better be identical). Of course, a good compilation framework eliminates the unnecessary copies.

  • Static data members of class templates: These can be particularly rough on the linker, but that's not your problemyou just define them in your header file and let your compiler and linker deal with it.

Also, a global data initialization technique known as "Schwarz counters" or "nifty counters" prescribes planting static (or unnamed-namespace) data in a header file. Jerry Schwarz made the technique popular by using it to initialize the standard I/O streams cin, cout, cerr, and clog.

    Team LiB
    Previous Section Next Section