Jump to content
  • Advertisement
Sign in to follow this  
youshotwhointhewhat

Return value optimization and self reference

This topic is 3417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!