Johannes Bauer schreef op 12-Mar-15 om 10:05 AM:
> On 12.03.2015 09:37, Wouter van Ooijen wrote:
>
>> Template programming is one of the things in C++ I like most, and IMO it
>> is one of the most powerfull tools in writing re-useable code for very
>> resource constrained systems.
>
> Ooooh I think template programming is dreadful. Not because of the
> results, the results are really amazing.
>
> But because of the debugging and readability issues. Oh and the horrors
> of compiler errors with templates. It's dreadful.
As with all features, it is important how they are used. When I write a
class template, the very first lines check the classm arguments for
suitability. The error messages are still not as clear as they are for a
straight error in your code, but you only need to look at the first
three error lines (the assert failure, dismiss the location of that
assert, and the next line is the template invocation).
> And for resource constrained systems, templates are the aboulte worst
> because they introduce a TON of code duplication.
Again, not when used correctly. I mostly use templates for very thing
Adapters, where inlining saves more code than the duplaction costs. But
in other situations I derive (or implemnet) the template class
from/on-top-of a not-template base class. I guess this pattern has a
name, but I don't know it.
> I'm not sure what
> systems you're referring to, but I'm wokring on Cortex-M
> microcontrollers. So let's say a Cortex-M0 with 64k flash.
That's a *large* chip in my book. I mostly use LPC1114 level chips.
> I dare to to
> use one, only a single one of those templated STL library functions and
> your ROM will be full already.
STL offers run-time flexibility in the size of what you are
storing/handling/computing. If you need that flexibility, OK, but most
small systems don't: they have a fixed-sized task that must be done, no
matter what. Doing more is not a plus. So the STL approach as it is
*implemented* in the current STL is totally unsuited for (very) small
systems.
But some of the basic principles could be very usefull, if implemented
appropriately (fixed-maximum-size containers, allocated without heap).
>
>> I agree that large parts of the C++ libraries (and even some parts of
>> the language) are unsuited to programming very small systems. But even
>> with those features discarded I still like C++ better for such work than
>> anything else.
>
> I just tested it, have a project here that does a ton of work on a
> Cortex-M3, compiled in C. 62k of code, with many parts of the C standard
> library in there (printf and friends). Now I added one single file:
>
> #include <string>
> #include <iostream>
> void foo(const std::string &moo) {
> std::cout << moo;
> }
>
> And linked the whole thing again with g++. First observation: New
> syscalls are drawn in. kill, getpid and open. What? kill and getpid,
> seriously? What is going on? In any case, after implementing stubs for
> those, it links: 293 kB of binary! That is almost a FIVE FOLD increrase
> in code side fo doing pretty much nothing.
I agree the the implementation of std:ostream *sucks* for using on small
systems. But I do like the opeartor<< approach, so I have been
struggling to find alternatives. So far no real solution, but someday...
> I must admit that I expected horrible results, but the sheer amount of
> useless code that is pulled in actually did shock me. And I double
> checked to see if my measurement was correct, and it was. The code is
> full of monstrosities like
When you switch to the desktop-mindset (memory is cheap, but only cache
is fast, average performance is what counts, and better be safe (catch
exceptions) that sorry) the design choices that produce this result are
not that weird. It is just that small embedded systems require a very
different mindset.
>> I am experimenting with (virtual!) inhertitance in combination with the
>> de-virtualization available in recent gcc and I am positively amazed by
>> the results. In the appropriate situations (where direct calls would be
>> using in C) the compiler eliminates all VFT's and can even be persuated
>> to inline the calls.
>
> Sounds very interesting. Do you have any pointers, I wasn't aware of
> that feature. It sounds like something like that could be very useful
> for very small systems indeed.
No, I have not found much beyond the very technical descriptions of how
it is implemented. I use the latest ARM GCC from
https://launchpad.net/gcc-arm-embedded , with -fdevirtualize. I compile
'all-at-one', which might or might not be needed.
I did a talk on Meetting C++ about my previous approach, based purely on
templates called "objects? no thanks":
http://meetingcpp.com/index.php/tv14/items/14.html
IMO C++ is a wonderfull language for developing code (especially
re-useable code) for very small systems, but is difficult to find the
correct subset of the language, libraries and programming paradigms
(juk, I hate that word) because
1. the vast majority of C++ users work in different fields and hence
have very differnt priorities
2. many very-small-systems developers don't appreciate the advantages of
C++ and are scared away by the horror stories of linking in large libraries.
Wouter van Ooijen