Embedded C++: Should I Adopt C++17?
C++17, the latest version of C++, is not as widely adopted. For that reason, we explain what is C++17 and what it means for embedded C++.
Read along or jump ahead to the section that interests you the most:
- What Is C++17?
- How Do Programmers Use Embedded C++?
- Should I Adopt C++17?
- What Are the Pros and Cons of C++17?
- Are C++17 Features Supported by MISRA and High Integrity C++?
➡️ Register for a Free Static Analysis Trial
What Is C++17?
C++17 is a revision of the ISO/IEC 14882 standard for the C++ programming language.
How Do Programmers Use Embedded C++?
According to a recent Embedded Systems Survey by the Barr Group, “Nearly 95% of embedded programmers wrote the majority of their code in C or C++”.
The popularity of C++ for embedded applications is growing while the language continues to evolve. The most recent version was published in late 2017.
Adopting New Programming Languages for Embedded C++ Systems
Historically, the adoption of new languages and standards has been slow.
As a young embedded C developer in the early 1990s, I attended a training course, “Object Oriented Programming in C++”.
Shortly afterward I switched jobs and began to develop C++ applications for Windows.
Now — 25 years later — I find it noteworthy (though perhaps unsurprising) that C is still the primary language for over 70% of embedded systems!
The adoption of C++ has been slow. And it takes time for new versions of the language to be adopted.
Should I Adopt C++17?
In the past, a significant barrier to the adoption of a new language has been the length of time taken for platforms and tools to catch up.
This has changed.
According to prominent C++ expert Herb Sutter:
We see at least one major implementation that has all C++17 features in the same year that [it] is published…all of the major compilers’ current releases are in closer sync with the standard than they’ve ever been before.
Does the availability of compiler support mean that embedded developers will adopt the new C++ language straight away?
It's possible but unlikely.
4 Questions to Ask Before Adopting C++17
You'll need to weigh the benefits of the latest version of C++ with potential drawbacks.
When considering whether you should adopt the latest version of C++, here are four questions to consider:
1. Will it save development time?
2. Will it reduce code maintenance costs?
3. Will it improve runtime performance?
4. Will it reduce safety and security risks?
What Are the Pros and Cons of C++17?
The latest version of C++ includes features that improve your code, performance, and security. While these are nice features, they're not earth-shattering.
One of the biggest additions is the introduction of the parallel algorithms library.
Multi-processor systems are required for computer-intensive programs and artificial intelligence. The parallel algorithms library makes it easier to execute standardized algorithms on this type of system.
There are two features that will enable cleaner code:
- Selection statements with initializer.
- Structured bindings.
This also reduces keystrokes for those who understand the syntax.
So, these features provide a step up for cleaner code.
📕 Related Resource: Coding best practices for C++
“Guaranteed copy elision” is designed for improved compiler optimization. So, it may improve runtime performance.
Safety and Security
The latest C++ features will improve the safety of your program and ensure secure coding, including the following:
- Removal of trigraphs and dynamic exception specifications.
- Stricter order of expression evaluation.
- Introduction of std::byte type.
The first two features prevent unspecified or undefined behavior.
The std::byte feature improves type safety. It distinguishes byte-oriented access to memory from accessing memory as a character or integral value. It also improves readability. The intent of the code is clearer.
📕 Related Resource: Learn more about the changes from C++14 to the latest version of C++
Are C++17 Features Supported by MISRA and High Integrity C++?
Compliance can be a barrier to adoption. And in order to adopt a new version of embedded C++, you need to be certain that you can use it and still be compliant. And that means having a coding standard that supports your language.
C++17 and MISRA
Many of the new features are already covered by those using Helix QAC for MISRA C++ compliance:
- Rule 2.3.1 “Trigraphs shall not be used”
- Rule 5.0.1 “The value of an expression shall be the same under any order of evaluation that the standard permits”
- Rule 15.4.1 “If a function is declared with an exception-specification, then all declarations of the same function (in other translation units) shall be declared with the same set of type-ids”
📕 Related Resource: Learn more about MISRA C and C++
C++17 and High Integrity C++
There are also equivalent rules in the High Integrity C++ (HICPP) standard (developed by Perforce static analysis experts):
- 2.2.1 Do not use digraphs or trigraphs
- 5.1.2 Do not rely on the sequence of evaluation within an expression
- 1.3.5 Do not use throw exception specifications
Move to Modern C++ With Perforce
Interested in moving to modern C++? Learn how to do it by visiting our resource page on how to move to modern C++.