… well actually it doesn’t. But it sounded good when I made it up. So recently I found myself needing to use a C++ partial function specialisation. No-one was as surpsied as me when this happened.
To say I was anti-templates would be too strong. I find the code they produce hard to follow, regardless of how elegant and efficient they really are. But then they do serve a really useful need. Obviously containers are an example of something where templates are indispensable. So you can’t just write them off.
But now that we have the standard library (and boost) do mere mortals really need to worry or bother about templates in the large? I think the answer to this is probably no.
But then again it depends on what you’re doing. And since I’ve started working on writing a wrapper around the TIBCo/RV library for Ruby I found myself writing a lot of C++ code that was very similar apart from the types involved. So then a little light bulb goes on in my head (40w supermarket own brand, cliche retardant) . Surely I can write a template to do this for me? This will mean that there’s only one code base for a bunch of similar behaviour and when I need to write that internationisation add-on (i18n) for my error messages I’ll only need to change one or two methods instead of a zillion.
So I did the only honourable thing. I broke out my trusty Stroustrup 2nd edition. Found the footnote about templates. Grok’d the syntax again and I was away. Shortly after I realised that in one of functions the integer processing needed bounds checking and the floats didn’t/couldn’t. So I needed some different behaviour for float’s and int’s. So, what I really needed now was some function templates that explicitly did something for floats and treated everything else the same. Great, no problem. Explicit instantiation is your friend. Add a dash, mix well, retire.
The interesting part, at this point, was that I’d arrived in a new place. A place where perhaps templates weren’t so bad after all. So then I got to thinking what I really needed was code that could distinguish between signed and unsigned integers at compile time and surely there must be a way to do that. And maybe if I partially specialise this function a bit and explicitly specialise that one a bit it will just magically work.
Since the project I was working on thus far had been written without the benefit of real objects I decided at the begining I only needed function templates to do the job. Now to recap, I have just discovered that I needed partial specialisation of those templates to do different things for signed and unsigned ints. Well like I say, no-one was as surprised as me to find myself here.
But as so often happens with C++ it’s always ready to kick you in the balls at any moment. I couldn’t get the expected (guessed) partial function specialisation syntax to work. So, a quick search of the web sought out this jewel that was predicatbly written by the man Sutter which explains the situation in depth. Now I’m not going to rehash Sutter’s explanation. He does it well, so there’s no need.
But it’s like a drug, the next step would be to either use plain old functions (not possible in this case) or change my function templates into class templates to avoid the issue. And before you know it you’ve just stepped a little further away from the target … sometimes this is a good thing and sometimes it’s a bad thing. But with C++ you’ve got to watch it because before you know it you’ve templatized every living thing and no-one, not even god can tell you what you’ve done to yourself.
C++ is a weapon. The US military probably wants to classify it as lethal. But then they’d probably end up killing Europeans with it, dude.
Sometimes you’ve got to sit back because the solution is far more complicated than the problem it solves. And so I’m sitting back. I’m good at that. Very good indeed.