Electronic – New C++ (C++11) and embedded electronics

cembeddedprogramming

I wonder if the new C++ (which called C++11) works well with the embedded electronics and programming them. Do the new features fit well if working with uC? Like R-Values and so on? Or should be restricted with the traditional and old-style C++?

Best Answer

It is not C++11 or old-style-C++, just like it is not C-only or C++-using-all-its-features. I love to use C++, but I hate specific aspects of it. (This is not C++ specific, although there are languages that I hate without exception.) Take the good bits (after checking that they are decently implemented), leave the rest for others or for later.

I have not used C++11 specifics yet (my lessons for this quarter are C++ on NDS using devkitPro, which has an old gcc). But one simple feature that I look forward to is the auto typed variable. Suppose you want to construct different types of objects (all subclasses of one base class), depending on the types of the 'constructor' parameters. You can't overload constructors of different classes, but you can overload various functions that return different types of classes. But to store the results, you must either remember the exact type they return (which spoils the factory pattern IMO) or have them all return a pointer to the base class type (which pulls heap management into your application, which I try to avoid). With the auto feature you can do

a_very_long_and_difficult_to_remember_class f( int x );
an_equaly_difficult_to_remember_class f( char *p );

auto x = f( 12 );
auto y = f( "hello" );

To me this means that an attractive pattern is suddenly very easy to use.