Skip to content

Instantly share code, notes, and snippets.

@shomah4a
Created September 23, 2010 06:57
Show Gist options
  • Save shomah4a/593252 to your computer and use it in GitHub Desktop.
Save shomah4a/593252 to your computer and use it in GitHub Desktop.
#include <iostream>
template <typename V, typename N>
class Vector
{
public:
const V value;
const N next;
Vector(const V& v, const N& n): value(v), next(n) {}
Vector<V, N> operator +(const Vector<V, N>& left) const
{
return Vector<V, N>(this->value+left.value, this->next + left.next);
}
};
template <typename V>
class Vector<V, void>
{
public:
Vector(){}
Vector<V, void> operator + (const Vector<V, void>& left) const
{
return *this;
}
};
typedef Vector<float, void> END;
typedef Vector<float, END> Vec1;
typedef Vector<float, Vec1> Vec2;
typedef Vector<float, Vec2> Vec3;
Vec2 makeVec2(const float x, const float y)
{
return Vec2(y, Vec1(x, END()));
}
Vec3 makeVec3(const float x, const float y, const float z)
{
return Vec3(z, Vec2(y, Vec1(x, END())));
}
int main(int argc, char* args[])
{
const auto v1 = makeVec3(1.0f, 0, 0);
const auto v2 = makeVec3(0, 1.0f, 0);
const auto v3 = makeVec2(1.0f, 0);
const auto r = v1 + v2;
// const auto err = v1 + v3; // Error
std::cout << "z: " << r.value << std::endl
<< "y: " << r.next.value << std::endl
<< "x: " << r.next.next.value << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment