C++ Coding Standards

The following is not intended to generate a holy war, but merely a place for me to remind myself of the decisions I took along the way to get to a consistent style for any new code that I write.


Tabs set to 4, and set it to replace all tabs with spaces. Shift-Tab will backspace a tabs worth of spaces. Using spaces is so the code will look the same when cut and paste into other environments, like these blogs, email, and also online code links to godbolt.org


-Wall and then turn off using pragma for any that are not needed.

Use C++ 11 for a simpler life and good compatibility. I am unlikely to use any of the new features. Well I do like if constexpr.

I found another nice C++17 feature. The default template declaration. It would mean you can have a class like cString<allocator=heap> and then call it without the template brackets.

enum allocator::source { heap, stack, new, malloc };
// No need for a custom source as it would just be added here.
// ...
cString<allocator::heap> test;
cString<> testUsingDefault;
cString<allocator::stack> faster;
// and replace it with
cString test;
cString<allocator::stack> faster;


Allman style with forced braces even for one line. Collapsing code into one neat little line is very tempting, but key reasons not to is for code readability and allowing for a place to step inside the debugger. Vertical space is not that critical if the code is clear and, simple and broken down to low Cyclomatic Complexity. I keep trying and can never get on with K&R style and its variants.


Take your pick from: camelCase, PascalCase, snake_case, SCREAMING_SNAKE_CASE.

My preference is camelCase as that is easier to type the snake_case, but the later feels more C++. Modern C++ have moved away from SCREAMING_SNAKE_CASE and I think that is a good move.

I tried snake_case for a while and the underscore is not that much of an issue because of the autocomplete. I still feel that camelCase is more readable and natural. It also takes up less space on a line.

camelCase it is then.

Hungarian or English?

I grew up with strong Hungarian notation and it makes sense in C where the typing is very weak. For modern C++ there is a view that this is not necessary any more as the compiler will warn of any conversions. That leaves member variable and static/global variables. While the m_ approach is nice, if you do that, then why not for everything.

My solution was arrived at by realising the member and static variables are to some degree special and that this can follow the direction indicated by the reserved cases for identifiers (__example, _ANOTHER). Thus, while using underscore is frowned upon, I like the notation to show the intent that these things are ‘part of inner code’. The same approach is to be used for static variables in the class or struct scope and also for local functions that clash with standard library versions.

class entity
    static constexpr size_t     _maxEntities = 1000;
    entity(char* name)
        _name = name;
    char    _name[8];

inline float _floor(const float& a);

For true global variable, use a single global struct call ‘g’. This allows for a central way to create a warning that these values must be written to very carefully.

struct globals
    int     count = 0;
    bool    isGameRunning = false;
struct globals g;
g.count++;    // Not thread safe.

camelCase can also be used for filenames. This is in contradiction to keeping all filenames lowercase to help with any accidental file use under windows due to case-insensitivity. But for consistency sake and a nice look it will be used here as well.


Naming Conventions

size_t  n;          // For bytes in a buffer
int     i;          // For loops etc only
size_t  index;      // For array index - don't use 'i'
size_t  length;     // For char* only
size_t  size;       // For number of items in classes
size_t  capacity;   // For current max number of items
type    rhs;        // For operators overloads with lhs and rhs

Use nullptr over NULL to get the type information for the compiler.



Align clasess and structures to 16 bytes unless they only contain char.

If using other backing memory then align to 16 bytes and make sure the class or struct is a multiple of 16 using packing

Comments from Various Sources

I’m a contract videogame programmer, so the answer is: I follow whatever the company guidelines call for. Typically, videogame code doesn’t use RTTI or exceptions, and follow CamelCase naming rules, m_ member variables, s_ static variables, and tab=4s. It’s remarkably consistent across the industry, for some reason. BoarsLair

Member Function Partial Template Specialisation

I was messing around implementing a hash function to be able to do the equivalent of std::map_unordered. I realised that I would like to have some functions specialised but that most of them would be OK use the default implementation. I tried to do this and found the compiler would not let me. Google helped out and I found that this is not in the language. So I thought that this must have a community accepted idiom to achieve the same thing. This is the story of that journey.

Continue reading “Member Function Partial Template Specialisation”

Buying a New Keyboard – The Mechanical Renascence

So you have an old Microsoft Wireless Desktop 3000 Keyboard and Mouse. You get used to it and it becomes your ‘personal standard’ for keyboards. It has full travel keypress and all the keys laid out the way you want them – the large Enter Key and the Media Mute key. But then it gets old and breaks and they don’t make that version anymore, so you look and the closest replacement. In this case the cheaper Microsoft Wireless Keyboard 2000. It’s OK and you put up with it, but the mouse starts to break as well. What to do…

Microsoft Wireless Desktop 3000 Reviews | TechSpot
Microsoft Wireless Desktop 3000 Keyboard (with the full size UK Enter Key)
Continue reading “Buying a New Keyboard – The Mechanical Renascence”

Learning C++

This is a series of things that I found out when learning C++. The approach is going to non-standard and likely to annoy any normal C++ professional. I am interested in languages in general and so I will be doing all my early code without and STL at all. I want to learn what features the language offers before starting to use the standard libraries. To this end I try to do do simple version of the features that are already available. That way I can see how these features are implemented in the standard libraries. To me that feels like a important advantage in learning the language. I did try to read the code for the libraries but it has a lot of very nuanced code that I will only get to understand much later on. The code is still interesting to step through however as it does offer clues as to how to unpick various implementations.

Continue reading “Learning C++”

A Star Path Finding Comparison to AI Techniques

The A Star algorithm defined as “best-first, graph search algorithm that finds the least-cost path from a given initial node to one goal node (out of one or more possible goals)” (INAM, 2009). It is used throughout the games and AI industry for many different reasons, but mainly used for runtime pathfinding for NPC’s in games. Out of the possible methods for pathfinding this is used in most cases due to its guaranteed accuracy to find the shortest path on any map.

Continue reading “A Star Path Finding Comparison to AI Techniques”

Streaming a Window Without Hardware

Given what OBS does (screen grab and encode) it would seem that there should be a simple way to capture a window and create a local video stream from it. This could then be viewed remotely served from your home PC, or used as an input in the composition for a stream scene.

This is clearly done using a HW video capture device, but that is expensive and given everything is already digital, seems like an odd detour.

An example use case for this is if you were using a separate Windows laptop of tablet for drawing and wanted to be able include it in a Twitch stream. Web cameras do this and use the RTSP protocol. So I went looking for a simple way to set this up on a new machine.

Continue reading “Streaming a Window Without Hardware”