I’m not sure what’s going on – wordpress stats shows that some people saw this post when it was a draft. I thought it’s not possible, but… I’m a little sleepy so maybe I’ve done something wrong (or better – not 100% correct :-) ).
Second thing is about avatar – I’ve uploaded one, but it’s not visible on “about” page and in my posts. If you have any idea – please let me know… I’m just a programmer, you know :-).
Now, let’s get to the point.
IAR templates support
On IAR site you may read that their Extended Embedded C++ supports templates. It does. Mainly. But there are (of course) some funny issues.
Let’s start with something simple, there shouldn’t be any problem:
template <typename TypeA>
class ExampleClass
{
TypeA W;
};
and somewhere in code...
ExampleClass <int> Instance;
Here you go, first template. Working, how sweet. Let’s try something a little bit complicated.
template < typename TypeA >
class BaseClass
{
/*code*/
};
class DerivedClass : public BaseClass < DerivedClass* >
{
/*code*/
};
DerivedClass inherits from the BaseClass template. The template parameter is a pointer to DerivedClass.
This example will compile under IAR and gcc. Anyway, try to use our DerivedClass.
///somewhere
DerivedClass Instance;
Such code crashes IAR. Updating compiler, linker, even downloading whole newest workbench doesn’t fix the problem. There were “internal tool error”, “fatal tool error” and crash without any message (IAR just disappeared).
Same code working after compiling with gcc without any problems.
Workaround…
Because there can’t be pointer to derived class in template parameter, pointer to void could be used. This solution should work, but it’s ugly. So there is nice one:
template < typename TypeA >
class BaseClass
{
/*code*/
};
class DerivedClass;
class DerivedClassWA : public BaseClass < DerivedClass* >
{
};
class DerivedClass : public DerivedClassWA
{
/*code*/
};
///somewhere
DerivedClass Instance;
This code is compiling and working under IAR because there is no pointer to derived class as template parameter. It should be correct for any C++ compiler.