Skip to main content
2 of 3
added 177 characters in body
Matt
  • 1k
  • 2
  • 7
  • 10

Is it bad practice to write code that relies on compiler optimizations?

I've been learning some C++, and often have to return large objects from functions that are created within the function. I know there's the pass by reference, return a pointer, and return a reference type solutions, but I've also read that C++ compilers (and the C++ standard) allow for return value optimization, which avoids copying these large objects through memory, thereby saving the time and memory of all of that. Now, I feel that the syntax is much clearer when the object is explicitly returned by value, and the compiler will generally employ the RVO and make the process more efficient. Is it bad practice to rely on this optimization? It makes the code clearer and more readable for the user, which is extremely important, but should I be wary of assuming the compiler will catch the RVO opportunity?

Edit: I don't believe this question is about micro-optimizations as was suggested. Is this a micro-optimization, or something I should keep in mind when designing my code?

Matt
  • 1k
  • 2
  • 7
  • 10