最近

研究でCG扱うことになってきたので、C++でかっちりした自分用のライブラリでも書こうと。以前からまー作ろうかなぁと思ってたのもあったんで、結構マジで。

第1弾はベクトル。
まー、3D-Graphics扱うなら必須だよねぇ。。。ってことで。

しかもfloatとdoubleとlong doubleと使い分けられるように、とか思い初めてtemplateバリバリで。
そーするとさー、
CVector4D v1;
CVector4D v2;
ってなってたら、v1 + v2はCVector4Dとかになってて欲しいじゃん・・・どこで使うんだかわからんけど^^;。電車で移動中とかずーっと考えててできた解決策。

template<typename T> class CVector4D {
public:
    // 中略
    template<typename U>
    CVector4D(const CVector4D< U >& v) {
        // 略
    }

    template<typename U>
    CVector4D<T> operator +=(const CVector4D< U >& v) {
        // 略
    }
};

template<typename T, typename U> class CFloatConversion;

template<float, double> class CFloatConversion {
public:
    typedef double SuperType;
    typedef float SubType;
};
template<double, float> class CFloatConversion {
public:
    typedef double SuperType;
    typedef float SubType;
};

// などなど。他略

template<typename T, typename U>
inline
CVector4D<typename CFloatConversion<T, U> :: SuperType>
operator +(const CVector4D<T>& v1, const CVector4D< U >& v2) {
    CVector4D<typename CFloatConversion<T, U> :: SuperType> result(v1);
    result += v2;
    return result;
}

長い・・・。
足し算1個が8行。引き算も。スカラー積も。まじすか。