should i adopt modern c++? · bertil spolander, senior software engineer. agenda •languages in...

25
Should I Adopt Modern C++? Bertil Spolander, Senior Software Engineer

Upload: others

Post on 13-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Should I Adopt Modern C++?

Bertil Spolander, Senior Software Engineer

Page 2: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Agenda

• Languages in Embedded Systems

• Requirements on your software project

• Advantages of C++

• Preconceptions of C++

• Modern C++

• Take care

• Summary

Page 3: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Languages in Embedded Systems

Page 4: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

What languages are used in Embedded

Systems today?

Page 5: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Requirements on your

software project

Page 6: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

What’s important

• Small code footprint

– Saves flash and thus money

• Fast execution

– Keep up with peripherals

– Better user experience

Page 7: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

What’s important (cont.)

• Have a good design

– Easier to maintain

– Easier to extend functionality

• High quality

• Time to market

– Earlier return on investment

Page 8: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Advantages of C++

Page 9: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

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.

Page 10: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Preconceptions of C++

Page 11: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Preconceptions of C++

• Bloated

– Code size

– Data size

• Inefficient

– Pure performance and reliability of the performan

– Uses the heap to much

• Hard to debug

Page 12: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Modern C++

Page 13: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

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

Page 14: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Modern C++, features

• Efficiency

– Move semantics

– Constant expressions

• Conveniance

– Range based for-loops

– Automatic type inference

– Attributes

Page 15: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Temporary objects

Page 16: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Move semantics

Page 17: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

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.

Page 18: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

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;

Page 19: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

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

Page 20: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Demonstration, debug support

Page 21: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Use with care

Page 22: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Careful…

• Run time type information (RTTI)

• Exceptions

• Standard library

– Particularly STL

Page 23: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Summary

Page 24: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

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

Page 25: Should I Adopt Modern C++? · Bertil Spolander, Senior Software Engineer. Agenda •Languages in Embedded Systems •Requirements on your software project •Advantages of C++ •Preconceptions

Thank you for your attention!

www.iar.com