I'm writing a class Vector3<T> to represent a threedimensional vector with elements of type T. I am doing this as a practice for C++ operator overloading. Turns out to contain some template lecture as well. :-)
I have typedefed some variants for my class like so:
Now, I can even implicitly go from a Vector3f to a Vector3d using a type cast function template. However, the other way around requires an explicit cast and I am still on non C++11 here, so I write a function for it:
Then I can write this: Vector3i intVec = Vector3d(3.2, 5.4, 9.5).as<int>();
However, I'd much prefer if I could instead have the call look like this: Vector3i intVec = Vector3d(3.2, 5.4, 9.5).as<Vector3i>();
Is it possible to do that?
Off-topic: Forum post preview function is failing me/shows garbage.
EDIT: Got "ERROR Invalid form (for-NT34)" from your board software on first try.
I was thinking the wrong way round. This is much simpler than anything I had tried to cook up. It's also a great example to show compile-time checking, since accessing OTHER_VECTOR::type can only work with OTHER_VECTOR being of a certain class, i.e. Vector3. It could still be any class which contains typedef of same name, but I think that's beyond sane chaos-control? Thanks!