Musings on C++

Composable Templated Vectors

In this blog entry, I present a code snippet demonstrating how a templated math vector class can have a construction interface which allows it to be composed of any number of smaller or equal-dimension vector types. It’s only requirement is that the value type is convertible to it’s own value type.

Over the last while I have been doing a fair bit in OpenCL at work. It has been quite an interesting experience in discovering where performance bottlenecks lie when coding for the GPU.

However, all that aside, there was one feature in OpenCL which I found quite handy: how composable the vector types are.

It’s quite handy and for a brief moment I considered all the C-style math libraries I have used during my time writing C++ code in the video games industry and lamented that I didn’t have anything this handy for C++. So I figured why not try implement it and see how it comes out.

To get great composability, it’s really just a variadic template for one constructor. It would need to use SFINAE to restrict its applicability so it doesn’t consume any parameter types which the vector clearly cannot be composed with, and allow other constructors to be able to handle those cases.

Furthermore there is room to report error conditions with static_assert (such as not supplying enough parameters).

Now Visual Studio 2013 has a few bugs in it. It seems to be a little fragile when it comes to variadic template expansion. I had a few internal compiler errors where GCC was quite happy with the code. So some of the code is more verbose than it needs to be. Were VS2013 as compliant as GCC is, I wouldn’t need a ‘detail’ namespace as the code therein would be located inside the vector class itself, and it would be more succinct too.

Thus the code is more verbose and looks a bit more messy than it ideally would be.

In a rather naive test I write some code using a typical C style struct for a vector, did some operations and then did similar operations with this class. For a release build with Visual Studio the assembly generated was pretty much the same (aside from a difference in choice of registers) so it at least indicates this should (as expected) be similar in performance.

Of course proper testing would be needed.

So heres the code for it – just the shell to demonstrate the aforementioned variadic constructor and swizzle functionality.