Author: Kipp E.B. Hickman

# C++ Tips

This is a compilation of tips on how to write cross-platform C++ code that compiles everywhere.

## General

- Always use the nspr types for intrinsic integer types. The only exception to this rule is when writing machine dependent code that is called from xp code. In this case you will probably need to bridge the type systems and cast from an nspr type to a native type. The other exception is floating point; nspr defines PRFloat as a double (!).
- Exceptions do not work everywhere so don't use them anywhere except in machine specific code, and then if you do use them in machine specific code you must catch all exceptions there because you can't throw the exception across xp code.
- Templates do not work everywhere so don't use them anywhere.
- Do not wrap include statements with an #ifdef. The reason is that when the symbol is not defined, other compiler symbols will not be defined and it will be hard to test the code on all platforms. An example of what not to do:

`#ifdef X`
`#include "foo.h"`
`#endif`
``

- For types that do not need operator= or a copy constructor, declare them yourselves and make them private. Example:

`class foo {`
`...`
`private:`
`// These are not supported and are not implemented!`
`foo(const foo& x);`
`foo& operator=(const foo& x);`
`};`

## Windows Compatability

## Metroworks Compatability

- MAC compilers do not handle #include path names in the same manner as other systems. Consequently #include statements should not contain path names, just simple file names. An example of what not to do:

#include "gemini/nsICSSParser.h"

## G++ Compatability

- Use void in argument lists for functions that have no arguments (this works around a bug in g++ 2.6.3)