What features of C++ should be avoided in embedded systems?
Please classify the answer by reason such as:
Regarding code bloat, I think the culprit is much more likely to be inline than templates.
For example:
// foo.h
template void foo () { /* some relatively large definition */ }
// b1.cc
#include "foo.h"
void b1 () { foo (); }
// b2.cc
#include "foo.h"
void b2 () { foo (); }
// b3.cc
#include "foo.h"
void b3 () { foo (); }
The linker most likely will merge all the definitions of 'foo' into a single translation unit. Therefore the size of 'foo' is no different to that of any other namespace function.
If your linker doesn't do this, then you can use an explicit instantiation to do that for you:
// foo.h
template void foo ();
// foo.cc
#include "foo.h"
template void foo () { /* some relatively large definition */ }
template void foo (); // Definition of 'foo' only in this TU
// b1.cc
#include "foo.h"
void b1 () { foo (); }
// b2.cc
#include "foo.h"
void b2 () { foo (); }
// b3.cc
#include "foo.h"
void b3 () { foo (); }
Now consider the following:
// foo.h
inline void foo () { /* some relatively large definition */ }
// b1.cc
#include "foo.h"
void b1 () { foo (); }
// b2.cc
#include "foo.h"
void b2 () { foo (); }
// b3.cc
#include "foo.h"
void b3 () { foo (); }
If the compiler decides to inline 'foo' for you then you will end up with 3 different copies of 'foo'. No templates in sight!
EDIT: From a comment above from InSciTek Jeff
Using explicit instantiations for the functions that you know will be used only, you can also ensure that all unused functions are removed (which may actually reduce the code size compared with the non template case):
// a.h
template
class A
{
public:
void f1(); // will be called
void f2(); // will be called
void f3(); // is never called
}
// a.cc
#include "a.h"
template
void A::f1 () { /* ... */ }
template
void A::f2 () { /* ... */ }
template
void A::f3 () { /* ... */ }
template void A::f1 ();
template void A::f2 ();
Unless your tool chain is completely broken, the above will generate code only for 'f1' and 'f2'.