This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Is it possible to support C++ compatibility in the API?

There are certain design decisions that break C++, or at least the separation of concern between allocation of entities and the use of entities.

For example:

typedef app_timer_t * 	app_timer_id_t
 	Timer ID type. Never declare a variable of this type, but use the macro APP_TIMER_DEF instead

In C++ you'll normally separate the definition from the declaration.

If you want to make this compatible with C++, you'll need some constexpr magic:

typedef const app_timer_t* const_app_timer_id_t;

#define APP_TIMER_DEF_CPP(timer_id)                                          \
    static constexpr app_timer_t timer_id##_data = { {0} };                  \
    static constexpr const_app_timer_id_t timer_id = &timer_id##_data;

Note that constexpr is relatively old, you'll probably support most of the C++ code with this.

PS: I also saw the post at devzone.nordicsemi.com/.../ but I'm not so much criticizing the code, I just like to point you to the guys out there (like me) who use C++.

Related