Following may help you : https://ideone.com/Ap6WWt
template <typename T, int W, int H>
class MatrixStream
{
public:
MatrixStream(Matrix<T, W, H>& mat, const T& value) : mat(mat), index(0)
{
*this, value;
}
MatrixStream& operator , (const T& value);
private:
Matrix<T, W, H>& mat;
int index;
};
template <typename T, int W, int H>
class Matrix
{
public:
MatrixStream<T, W, H> operator << (const T& value) {
return MatrixStream<T, W, H>(*this, value);
}
T m[W][H];
};
template <typename T, int W, int H>
MatrixStream<T, W, H>& MatrixStream<T, W, H>::operator , (const T& value)
{
assert(index < W * H);
int w = index / H;
int h = index % H;
mat.m[w][h] = value;
++index;
return *this;
}
but overloading operator ,
is highly discouraged.
As said in the comment, you may use as alternative m << {1, 2, 3, 4, 5, 6, 7, 8, 9}
or m << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9
. (the first one is cleaner IMHO).