Sign in to follow this  
youshotwhointhewhat

Return value optimization and self reference

Recommended Posts

Currently I have two versions of many of my member functions, for example:
Matrix Matrix::transposedCopy() const
{
	return Matrix(data[0], data[4], data[8], data[12],
     	    	data[1], data[5], data[9], data[13],
     	   	data[2], data[6], data[10], data[14],
     	   	data[3], data[7], data[11], data[15]);
}

Matrix& Matrix::transpose()
{
	float tmp;
	for(int j = 1; j < 4; j++)
		for(int i = 0; i < j; i++){
			tmp = data[i+(j*4)];
			data[i+(j*4)] = data[j+(i*4)];
			data[j+(i*4)] = tmp;
		}
	return *this;
}
The first function returns a transposed version of the matrix by value, which should be efficient with RVO. The reason I have the second version is because I wasn't sure if RVO works in the following situation:
Matrix m;

...

m = m.transposedCopy();
If RVO does work in this situation then I could get rid of the second version of the function, but it seems unlikely to me. I am using gcc to compile.

Share this post


Link to post
Share on other sites
Keep the two versions. RVO only works when copy-constructing an object from an expression/function call.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this