好的,这很可怕,但与我对原始帖子的评论有关。
使用这种结构应该可以定义您需要的相关操作,但是编写所有适当的专业化将是很多工作。您可能还希望交换行/列。
最后定义矩阵肯定不像你原来的帖子那样优雅,但也许这可以改进,特别是在 C++11 中使用“自动”。
//-----------------------------------------------------------------------------
struct Unused {};
struct Imaginary {
Imaginary() {}
Imaginary(Unused const& unused) {}
};
struct MinusImaginary {
MinusImaginary() {}
MinusImaginary(Unused const& unused) {}
};
//-----------------------------------------------------------------------------
template <int I, int F = 0>
struct Fixed {
Fixed() {}
Fixed(Unused const& unused) {}
};
//-----------------------------------------------------------------------------
struct Float
{
Float(float value) : value_(value) {}
const float value_;
};
//-----------------------------------------------------------------------------
template <typename COL0, typename COL1>
struct Vector2
{
typedef COL0 col0_t;
typedef COL1 col1_t;
template <typename T0, typename T1>
Vector2(T0 const& t0, T1 const& t1)
: col0_(t0)
, col1_(t1)
{}
COL0 col0_;
COL1 col1_;
};
//-----------------------------------------------------------------------------
template <typename ROW0, typename ROW1>
struct Matrix2
{
typedef ROW0 row0_t;
typedef ROW1 row1_t;
Matrix2()
: row0_(Unused(), Unused())
, row1_(Unused(), Unused())
{
}
template <typename M00, typename M01, typename M10, typename M11>
Matrix2(M00 const& m00, M01 const& m01, M10 const& m10, M11 const& m11)
: row0_(m00, m01)
, row1_(m10, m11)
{
}
ROW0 row0_;
ROW1 row1_;
};
//-----------------------------------------------------------------------------
Matrix2<
Vector2< Fixed<0>, Fixed<1> >,
Vector2< Fixed<1>, Fixed<0> >
> sigma1;
const float f = 0.1f;
//-----------------------------------------------------------------------------
Matrix2<
Vector2< Fixed<0>, Imaginary >,
Vector2< MinusImaginary, Fixed<0> >
> sigma2;
//-----------------------------------------------------------------------------
Matrix2<
Vector2< Fixed<0>, Float >,
Vector2< Float, Fixed<0> >
> m3(Unused(), 0.2f,
0.8f, Unused());
// EDIT: Nicer initialization syntax in c++11
//-----------------------------------------------------------------------------
template <typename M00, typename M01, typename M10, typename M11>
Matrix2< Vector2<M00, M01>, Vector2<M10, M11> >
MakeMatrix(M00 const& m00, M01 const& m01, M10 const& m10, M11 const& m11)
{
return Matrix2< Vector2<M00, M01>, Vector2<M10, M11> >(m00,m01,m10,m11);
}
auto m4 = MakeMatrix(Fixed<0>(), Float(0.2f),
Float(0.8f), Fixed<0>() );