The intersection of C and C++ has
- no decent initializers, you'd need designated initializers on the C
side and constructors for C++
- no decent temporaries (compound literals for C and constructors for C++)
- no consistent way of passing dynamically sizes multi-dimensional
arrays to functions
- no type puning (union's are standardized differently between the
two)
- no consistent model for function inlining
not very attractive, at least for me
I'm using C++ (and c++0x at that) for doing very, very low-level work,
down to 8 bit microcontrollers. And while most people seem to think that
this is impossible, quite the contrary is true: There is a huge benefit
in using it (if it is used correctly). For 8 bitters, for example, I do
not use fancy stuff (virtual methods, inheritance etc), nor parts of the
STL. I use heavily low-level language guarantees, such as strong typing
and especially strongly types class enums (gotta love those!).
To give an example on how MCU code looks using C and how it's usually
done (this is an example from an actual project of mine):
TCCR1A = _BV(COM1A1); /* Clear on Compare Match */
TCCR1B = _BV(CS10) | _BV(WGM13); /* 1 / 1 */
TCCR0B = _BV(CS02) | _BV(CS00);
where the _BV() macro is defined to be _BV(x) (1 << (x)). All the
symbols are defined by the standard library, the lvalues are volatile
registers and the rvalues are basically just bit definitions of the
hardware's datasheet.
This is just horrible. Notice how the settings of two different timers
are set, the first two lines refer to Timer 1 while the third refers to
timer 0. Both have a prescaler, or clock selection. Notice that the bits
in the standard library for this AVR processor are named CSnm, where n
is the timer (0, 1) and m is the bitvalue (0, 1, 2, 3). Obviously it is
*very* error-prone, because setting the wrong one in either register
leads to weird (and hard to debug results).
In C++, whith smart constructs, you can basically say
TCCR1().PRESCALER() = T1Presc::CKDIV_2;
TCCR0().PRESCALER() = T0Presc::CKDIV_16;
and it will boil down to the *exact* same code with no performance
penalty at all, only added typesafety. The compiler will barf if you try
to put the wrong type into a field. I find that quite awesome and this
is actually a reason for me to prefer C++ over C for this type of work.
Best regards,
Johannes
--
Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <
[email protected]>