0

Could somebody guide me through what gets called in this scenario?

template<class T>
class mat4 {
public :

T matrix[4][4];

    mat4();
    mat4(vec4<T> i, vec4<T> j, vec4<T> k, vec4<T> t);
    mat4(const T input_matrix[4][4]);

    //~mat4();

    mat4<T> Transposed() const;
    void Transpose();

    mat4<T> Inversed() const;
    void Inverse();

};

With the above code if i call

mat4<float> matrix;
float mf[4][4];

//fill the float matrix here

matrix = mf;

Then i know that the 3rd constructor gets called (it's not explicit), but what is called beside that? A copy constructor for matrix that takes the temporary object created from the assignment operation? I'm debating whether or not i should create a custom assign operator for it or just let it handle itself. The custom operator would only copy the matrix into the mat4 and return a reference to the mat4, but if there's no overhead in the automaticly created assignment operator then i'd rather stick to that.

dreta
  • 1,016
  • 3
  • 13
  • 22

2 Answers2

1

Any decent return value optimization should reduce this to just a call to the third constructor. As long as you're compiling with optimization, there's no need to reduce readability to reduce overhead in this case.

Also, I don't see how a custom copy constructor and/or assignment operator will have less overhead than the compiler-generated ones; they may even complicate the optimization.

smocking
  • 3,689
  • 18
  • 22
0

If this matrix held a std::vector<T>(16);, modern implicitly-generated assignment operator would "steal" the contents of the temporary created from mf and would avoid the second copy. For an older compiler, you could write such an assignment operator yourself (see What is the copy-and-swap idiom?).

However, this matrix holds an array as a data member: implicitly-generated assignment operator, just like any assignment operator you could write, will have to actually copy (or move, if T is movable) each of the 16 T values individually (or with a single memcpy if T is trivially-copyable). Compilers may in fact optimize out the temporary and copy all 16 Ts directly out of mf into matrix.matrix, but it's a rather more complex optimization than a simple RVO. GCC seems to be doing it, in my test.

Community
  • 1
  • 1
Cubbi
  • 46,567
  • 13
  • 103
  • 169