Inverse Kinematics Demo

We’re going off topic a bit, so I think this is going to be my last post on templates unless @Xhaku is particularly interested.

I still might branch some of these off into a separate thread anyway, we shall see.


I wouldn’t really call myself an expert either - I don’t think that’s for me to judge, I think it’s for others to judge.
But I think it’s safe to say that I probably have more experience with templates than most.

I was waiting for someone to mention this.

SFINAE is feared because it’s a big scary acronym and the phrase “substitution failure is not an error” only makes sense in context.

I’m often forgetting it because it’s rare to actually have to invoke it unless you’re writing a particularly complex template library.
Fortunately I wrote a relatively simple explanation-by-example to remind myself when I forget.

It’s not that complicated really, basically the rule is:
“If a template parameter in a template function can’t be properly substituted without encountering a subtitution error,
that doesn’t cause compilation to halt.
Instead it causes the compiler to look for other function candidates instead
An error only occurs when no suitable candidates can be found.”

Or more simply:
If the compiler can’t substitute a template parameter on a function, it keeps trying other functions until it finds a suitable match or it runs out of candidates.

The trick is to think of type in terms of their interface (functions, members, type aliases), their rules (invariants, preconditions, postconditions) and their roles (container, functor).

Honestly the best recommendation I can make is learn Haskell.
Even if you never use Haskell after learning it, the Haskell way of thinking applies really well to templates.
Haskell is essentially built around the kind of type manipulation that templates were built for.

This is improving thanks to static_assert.
For example, here’s a case of using static_assert to provide a nicer error message for an invalid template instantiation:

If Integer + Fraction is greater than the number of bits available in uintmax_t then the type cannot be properly formed.
Normally this would be a silent bug,
but thanks to static_assert, such an instantiation won’t compil and, the compiler will spit out the error message:
“Platform does not have a native type large enough for UFixed.”

Definitely. I’m always pushing features that improve type safety.
If everything is type safe then there are some bugs that you just don’t have to worry about anymore.

One of my favourite examples being with scoped enumerations vs unscoped enumerations:

EnumType value = 5; // Error: 5 is not a value EnumType
EnumType value = EnumType::SpecificValue; // Success: meaningful and without fear of runtime bugs

Programming is all about puzzles.
A darn sight more puzzling than Zelda dungeon puzzles. :P


If we stopped using powertools then nothing would get done.
Never using templates because they’re ‘powerful and scary’ would be like never using pointers because you can corrupt RAM with them.

The C++ philosophy is to trust your users, not to treat them like babies who need childminding.
I think that’s a big insult to their userbase - it sends out the message “we don’t trust you to use these powertools responsibly, so we’re not giving them to you”.
It’s like saying “we’re not going to give you a kitchen knife because you might cut yourself or stab somebody with it” - incredibly patronising.

There will always be someone who abuses the system,
no matter how much you try to prevent it or limit it.
It’s better to provide the power to the people who know how to use it properly than deny it to them just because others can’t handle that power.

In fact, if anything I would have thought you’d take the ‘power’ approach rather than the ‘sandbox’ approach because Linux takes the ‘power’ approach and Windows/Mac take the ‘sandbox’ approach.

Mac and Windows say “let’s hide the console and the programming tools from everyone because they can’t be trusted to know how to use them”.
Linux says “trust the user with all the powertools, centre the whole system around the power of the command line”.

Yes @Pharap, let’s not DDOS this thread, I’ll try to reply in this place:

I’ve been looking into templates thanks to this thread and I feel I have some criticism I’d like to share again. (@carbonacat or anyone else can join in too of course)