Tips and Tricks – 5 Tips for Developing Real-time C++ Applications

Object-oriented programming has become a very popular and critical programming methodology for developing applications. While traditional embedded software development has mostly been developed using the procedural language C, C++ and other object oriented languages are slowly started to gain traction in real-time systems. In order to ensure that a C++ based applications doesn’t bloat code and makes the best use of object oriented techniques, here are ten tips for developing real-time applications for microcontroller based systems using C++.

Software development concept. C++ (C plus plus) programming language in binary code.

Tip #1 – Use constexpr for constants, not #define

When developing a C++ application for real-time systems, the constexpr keyword is superior to #define. The constexpr keyword is guaranteed to be a compile-time constant and has clearly defined type information unlike #define. For example, a developer that wants to create a version number in C might write:


In C++, the preferred method would be as follows:

constexpr unsigned int version_major = 1U;
constexpr unsigned int version_minor = 0U;
constexpr unsigned int version_release = 0U;

Notice that we can clearly define the const as an unsigned int. The constexpr keyword can also be used to create constants of other types such as int, float, arrays, etc.

Tip #2 – Use namespaces to organize and encapsulate application data

In C, developers tend to organize data either by using structures or by using the pre-processor to define constant values. In C++, developers can organize application data so that it is encapsulated, organized and easily readable by utilizing namespaces. A namespace in C++ can be defined by using the following syntax:

namespace data
unsigned int mydata;

The namespace in this case is data and has a single member mydata. Accessing the namespace can be done accomplished by writing:


which uses the scope resolution operator to specify that mydata is located within data. Anonymous namespaces, namespaces with a name such as data, are recommended for creating local static variables.

Tip #3 – Use nullptr over NULL

One problem in C that is always encountered is that NULL is defined in multiple library files and each definition is inconsistent. This often leads to confusion and potential issues with using NULL. In C++, a new keyword nullptr is defined that can be used to assign a pointer to null and also to check if a pointer is currently null. For example,

// by default is initialized to null
unsigned int * myptr;

if(myptr == nullptr)
// Do something to initialize pointer

Tip #4 – Use templates

A template allows a developer to create a function or class that can be applied to data of different types. This allows a developer to create a single function or class that is easily reusable and scalable for different applications. Templates can easily be created by using the template keyword preceding the function or class as follows:

void myFunction(const T&)
// Perform work on T

The function can then be used with different datatypes by doing the following:
myFunction(1); // Passing in an int
myFunction(3.14) // Passing in a float

Templates are a very powerful feature that dramatically improve code reuse and scalability.

Tip #5 – Use the built-in bool type

Using bool in C always felt like a sticky situation. Developers had to find the right library, stdbool.h, which doesn’t seem to be supported in every compiler and often forced developers to create their own definition. Even when bool was easily found, bool could hold a value beyond just a simple true and false because it was stored in more than just a single bit. Any value 1 or larger was considered true. Even worse, it is always confusing as to whether a developer should use TRUE, True or true! C++ has a built-in bool type that can only hold a value of true or false. Something very simple which makes programming an application simpler and can make the software easier to read.


There are many more changes and recommendations that developers looking to use C++ in their real-time applications should follow but these tips will help a developer get started.

Share >

1 thought on “Tips and Tricks – 5 Tips for Developing Real-time C++ Applications

  1. Good article, Jacob–Thanks for it.

    Two notes: the constexpr statements need the assignment operator, ‘=’:
    constexpr unsigned int version_major = 1U;

    Also, the use of templates should be considered carefully. They are great–I use them a lot–but one must know that, unlike a function, they can cause the compiler to create duplicate code in some cases. Not a reason not to use them, though. Many programmers don’t, thinking they bloat the code. Just be careful!

    (Overloaded functions are in a sense “duplicates”, but folks don’t worry too much about *them*. Very odd.)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.