Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 27 July 2012 - 03:36 PM

You could also create a temporary and hidden struct just for initialization, if you really want that syntax.

Something like this:
template <typename Type>
class MyMatrixRow
{
	 public:
	 MyMatrixRow(Type column1, Type column2, Type column3) :
				column1(column1), column2(column2), column3(column3) { }

	 Type column1, column2, column3;
};

template <typename Type>
class MyMatrix
{
	 public:
	 MyMatrixRow(MyMatrixRow<Type> row1, MyMatrixRow<Type> row2, MyMatrixRow<Type> row3)
	 {
		  data.resize(9);
		  
		  data[0] = row1.column1;
		  data[1] = row1.column2;
		  data[2] = row1.column3;

		  data[3] = row2.column1;
		  data[4] = row2.column2;
		  data[5] = row2.column3;

		  data[6] = row3.column1;
		  data[7] = row3.column2;
		  data[8] = row3.column3;
	 }

	 private:
	 std::vector<Type> data;
};

The structs passed in to the constructor are just used for constructing and then discarded.

The code:
MyMatrix<float> matrix = {{0.0f, 0.0f, 0.0f},
                  {0.0f, 0.0f, 0.0f},
                  {0.0f, 0.0f, 0.0f}};

Actually turns into:
MyMatrix<float> matrix(MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
                 MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
                 MyMatrixRow<float>(0.0f, 0.0f, 0.0f));

...and hopefully the entire MyMatrixRow() thing would be compiled out, but I don't know anything about compiler optimizing so I can't say for sure.

#5Servant of the Lord

Posted 27 July 2012 - 03:36 PM

You could also create a temporary and hidden struct just for initialization, if you really want that syntax.

Something like this:
template <typename Type>
class MyMatrixRow
{
	 public:
	 MyMatrixRow(Type column1, Type column2, Type column3) :
				column1(column1), column2(column2), column3(column3) { }

	 Type column1, column2, column3;
};

template <typename Type>
class MyMatrix
{
	 public:
	 MyMatrixRow(MyMatrixRow<Type> row1, MyMatrixRow<Type> row2, MyMatrixRow<Type> row3)
	 {
		  data.resize(9);
		  
		  data[0] = row1.column1;
		  data[1] = row1.column2;
		  data[2] = row1.column3;

		  data[3] = row2.column1;
		  data[4] = row2.column2;
		  data[5] = row2.column3;

		  data[6] = row3.column1;
		  data[7] = row3.column2;
		  data[8] = row3.column3;
	 }

	 private:
	 std::vector<Type> data;
};

The structs passed in to the constructor are just used for constructing and then discarded.

The code:
MyMatrix<float> matrix = {{0.0f, 0.0f, 0.0f},
              {0.0f, 0.0f, 0.0f},
              {0.0f, 0.0f, 0.0f}};

Actually turns into:
MyMatrix<float> matrix(MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
             MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
             MyMatrixRow<float>(0.0f, 0.0f, 0.0f));

...and hopefully the entire MyMatrixRow() thing would be compiled out, but I don't know anything about compiler optimizing so I can't say for sure.

#4Servant of the Lord

Posted 27 July 2012 - 03:36 PM

You could also create a temporary and hidden struct just for initialization, if you really want that syntax.

Something like this:
template <typename Type>
class MyMatrixRow
{
	 public:
	 MyMatrixRow(Type column1, Type column2, Type column3) :
				column1(column1), column2(column2), column3(column3) { }

	 Type column1, column2, column3;
};

template <typename Type>
class MyMatrix
{
	 public:
	 MyMatrixRow(MyMatrixRow<Type> row1, MyMatrixRow<Type> row2, MyMatrixRow<Type> row3)
	 {
		  data.resize(9);
		  
		  data[0] = row1.column1;
		  data[1] = row1.column2;
		  data[2] = row1.column3;

		  data[3] = row2.column1;
		  data[4] = row2.column2;
		  data[5] = row2.column3;

		  data[6] = row3.column1;
		  data[7] = row3.column2;
		  data[8] = row3.column3;
	 }

	 private:
	 std::vector<Type> data;
};

The structs passed in to the constructor are just used for constructing and then discarded.

The code:
MyMatrix<float> matrix = {{0.0f, 0.0f, 0.0f},
        {0.0f, 0.0f, 0.0f},
        {0.0f, 0.0f, 0.0f}};

Actually turns into:
MyMatrix<float> matrix(MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
           MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
           MyMatrixRow<float>(0.0f, 0.0f, 0.0f));

...and hopefully the entire MyMatrixRow() thing would be compiled out, but I don't know anything about compiler optimizing so I can't say for sure.

#3Servant of the Lord

Posted 27 July 2012 - 03:36 PM

You could also create a temporary and hidden struct just for initialization, if you really want that syntax.

Something like this:
template <typename Type>
class MyMatrixRow
{
	 public:
	 MyMatrixRow(Type column1, Type column2, Type column3) :
				column1(column1), column2(column2), column3(column3) { }

	 Type column1, column2, column3;
};

template <typename Type>
class MyMatrix
{
	 public:
	 MyMatrixRow(MyMatrixRow<Type> row1, MyMatrixRow<Type> row2, MyMatrixRow<Type> row3)
	 {
		  data.resize(9);
		  
		  data[0] = row1.column1;
		  data[1] = row1.column2;
		  data[2] = row1.column3;

		  data[3] = row2.column1;
		  data[4] = row2.column2;
		  data[5] = row2.column3;

		  data[6] = row3.column1;
		  data[7] = row3.column2;
		  data[8] = row3.column3;
	 }

	 private:
	 std::vector<Type> data;
};

The structs passed in to the constructor are just used for constructing and then discarded.

The code:
MyMatrix<float> matrix = {{0.0f, 0.0f, 0.0f},
						{0.0f, 0.0f, 0.0f},
						{0.0f, 0.0f, 0.0f}};

Actually turns into:
MyMatrix<float> matrix(MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
			 MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
			 MyMatrixRow<float>(0.0f, 0.0f, 0.0f));

...and hopefully the entire MyMatrixRow() thing would be compiled out, but I don't know anything about compiler optimizing so I can't say for sure.

#2Servant of the Lord

Posted 27 July 2012 - 03:35 PM

You could also create a temporary and hidden struct just for initialization, if you really want that syntax.

Something like this:
template <typename Type>
class MyMatrixRow
{
	 public:
	 MyMatrixRow(Type column1, Type column2, Type column3) :
				column1(column1), column2(column2), column3(column3) { }

	 Type column1, column2, column3;
};

template <typename Type>
class MyMatrix
{
	 public:
	 MyMatrixRow(MyMatrixRow<Type> row1, MyMatrixRow<Type> row2, MyMatrixRow<Type> row3)
	 {
		  data.resize(9);
		  
		  data[0] = row1.column1;
		  data[1] = row1.column2;
		  data[2] = row1.column3;

		  data[3] = row2.column1;
		  data[4] = row2.column2;
		  data[5] = row2.column3;

		  data[6] = row3.column1;
		  data[7] = row3.column2;
		  data[8] = row3.column3;
	 }

	 private:
	 std::vector<Type> data;
};

The structs passed in to the constructor are just used for constructing and then discarded.

The code:
MyMatrix<float> matrix = {{0.0f, 0.0f, 0.0f},
                        {0.0f, 0.0f, 0.0f},
                        {0.0f, 0.0f, 0.0f}};

Actually turns into:
MyMatrix<float> matrix(MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
             MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
             MyMatrixRow<float>(0.0f, 0.0f, 0.0f));

...and hopefully the entire MyMatrixRow() thing would be compiled out, but I don't know much about compiler optimizing so I can't say for sure.

#1Servant of the Lord

Posted 27 July 2012 - 03:34 PM

You could also create a temporary and hidden struct just for initialization, if you really want that syntax.

Something like this:
template <typename Type>
class MyMatrixRow
{
     public:
     MyMatrixRow(Type column1, Type column2, Type column3) :
                column1(column1), column2(column2), column3(column3) { }

     Type column1, column2, column3;
};

template <typename Type>
class MyMatrix
{
	 public:
	 MyMatrixRow(MyMatrixRow<Type> row1, MyMatrixRow<Type> row2, MyMatrixRow<Type> row3)
     {
    	  data.resize(9);
    	  
          data[0] = row1.column1;
    	  data[1] = row1.column2;
    	  data[2] = row1.column3;

          data[3] = row2.column1;
		  data[4] = row2.column2;
		  data[5] = row2.column3;

          data[6] = row3.column1;
		  data[7] = row3.column2;
		  data[8] = row3.column3;
     }

     private:
     std::vector<Type> data;
};

The structs passed in to the constructor are just used for constructing and then discarded.

The code:
MyMatrix<float> matrix = {{0.0f, 0.0f, 0.0f},
                                 {0.0f, 0.0f, 0.0f},
                                 {0.0f, 0.0f, 0.0f}};

Actually turns into:
MyMatrix<float> matrix(MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
								 MyMatrixRow<float>(0.0f, 0.0f, 0.0f),
								 MyMatrixRow<float>(0.0f, 0.0f, 0.0f));

...and hopefully the entire MyMatrixRow() thing would be compiled out, but I don't know much about compiler optimizing so I can't say for sure.

PARTNERS