should i adopt modern c++? · bertil spolander, senior software engineer. agenda •languages in...
TRANSCRIPT
Should I Adopt Modern C++?
Bertil Spolander, Senior Software Engineer
Agenda
• Languages in Embedded Systems
• Requirements on your software project
• Advantages of C++
• Preconceptions of C++
• Modern C++
• Take care
• Summary
Languages in Embedded Systems
What languages are used in Embedded
Systems today?
Requirements on your
software project
What’s important
• Small code footprint
– Saves flash and thus money
• Fast execution
– Keep up with peripherals
– Better user experience
What’s important (cont.)
• Have a good design
– Easier to maintain
– Easier to extend functionality
• High quality
• Time to market
– Earlier return on investment
Advantages of C++
Why C++ instead of C
• Better modularity, which comes from the ability to couple data with the
methods and operators that manipulate it.
• Reuse of code by inheritance. You can have existing code that you can
use while still be able to specialize and enhance it.
• Flexibility through polymorphism. Being able to use the same interface
to different types of objects makes it easier to maintain stable API’s.
• Higher levels of abstraction where you can hide the implementation to a
higher degree.
• Larger standard library which means less programming for you with
better tested code.
Preconceptions of C++
Preconceptions of C++
• Bloated
– Code size
– Data size
• Inefficient
– Pure performance and reliability of the performan
– Uses the heap to much
• Hard to debug
Modern C++
History
• C++ and Embedded Systems mid 1990’s
– Bloated, slow, hard to debug
• Standards 1998 and 2003, not much happened
• C++ 2011
– Major changes and additions to the language
– Continued with C++ 14 and C++ 17
• Modern C++ starts with C++ 2011
Modern C++, features
• Efficiency
– Move semantics
– Constant expressions
• Conveniance
– Range based for-loops
– Automatic type inference
– Attributes
Temporary objects
Move semantics
Constant expressions
constexpr int function(...)
• Enables the use of functions where you
can only use constants.
• Requires that the compiler can calculate
the value of the function at compile time.
Automatic type inference
• I want the type of that expressionstd::vector<int> myvector;
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
{
std::cout << ' ' << *it;
}
std::vector<int> myvector;
for (auto it = myvector.begin() ; it != myvector.end(); ++it)
{
std::cout << ' ' << *it;
}
auto yourfunction(int i)
{
int local;
...
return local;
Attributes
[[noreturn]] - This function will not return
[[deprecated]] - This symbol is deprecated
[[override]] - This function intends to override a function
[[final]] - This is should not be overridden
[[fallthrough]] - This is intentional, don’t emit a warning
Demonstration, debug support
Use with care
Careful…
• Run time type information (RTTI)
• Exceptions
• Standard library
– Particularly STL
Summary
Summary
• C++ is better than C for creating and maintaining high quality software.
• Modern C++ and modern tools make the old preconceptions not so prevalent anymore.
• Choose carefully what features you use to limit loss in performance
Thank you for your attention!
www.iar.com