CPP-Styleguide

C++ Styleguide

Disclaimer

This is my own styleguide, meant mainly for me. You don’t have to agree or follow anything that’s written here.

Formatting

Function Braces (1)

The opening brace of a function should be placed on the same line as the function definition, separated by a space from the class definition. The closing brace should be placed on the line following the last line of the function.

int main() {
    // do something
    return 0;
}

Function Braces (2)

Braces should not be used for a function if you are only declaring the signature.

int main();

Forward-Declarations (1)

Always forward-declare functions that you MAY need later at the start of a file. It takes a few seconds, and saves you some time later.

Forward-Declarations (2)

Always use extern when forward-declaring a function that is NOT defined in the same file. This informs the reader that the function is defined somewhere else.

Forward-Declarations (3)

Always use extern when forward-declaring a variable in C++, as just T variable; may initialise it.

Identation (1)

Identation should be done using TABS.

File Format (1)

Files should be formatted with UTF-8 and LF, regardless of operating system. Only exception is Powershell scripts, which should use UTF-16+BOM and CRLF.

Pointer Management (1)

Avoid smart pointers unless:

Pointer Management (2)

Store pointers as:

Pointer Management (3)

Always use unsigned char* instead of char* (or signed char*, if you for some reason need it to be signed).

Building

Position-Independent

All output binaries should be as position-independent as possible, regardless of usecase. (Excluding kernel-level programming, or binaries where size is a priority)
On Linux, position-independent binaries should be made with the use of -fPIC or -fPIE -pie flags for g++.

Debug Builds

Debug builds should be built with the following arguments:

Production builds should be built with the following arguments:

[!CAUTION]

Please note that -Ofast introduces aggressive optimization that my slightly alter floating point math, and may break some things/

We also recommend the following flags, to forcefully prevent dangerous behaviour, as well as unused code:

[!TIP]

With the following flags, the presence of any unused parameters will throw an error. Use the following macro to suppress said errors:

#define UNUSED(x) ((void)(x))

Example:

void output(int number, int number2) {
   UNUSED(number2);
   std::cout << number;
}

However, only do this if you plan on shortly starting to use the parameter in question, or have no choice but to keep it.

Stack size

For builds making excessive use of alloca(), consider giving them a bigger stack size.

Naming

Naming conventions that should be used:

Temporary variables must start with an underscore, or end in _tmp.

Reproducibility

For the sake of reproducibility, only use compilers that are part of the GNU compiler set (gcc/g++/ld/nasm).

UB

UB (Undefined Behaviour) is a scary topic for everyone.
You shouldn’t avoid it. You should understand what happens when you run different mechanisms that cause ‘UB’, and you should avoid behaviour that is unpredictable/unexpected/error-prone/unreproducible.
Understanding the compiler is always better than fearing the compiler.