How to check if the return value has been optimized?

Consider this function:

std::string 
myClass::myFunction2() {
  std::string result = myClass::myFunction1();
  return result;
}

I hope the compiler optimizes the return value . How can I make sure that this is really happening and the code will not copy the result redundantly?

+4
source share
1 answer

RVO is always applied, if possible. For your case, if myFunction1 () does not return different named objects depending on the execution path, the compiler should execute RVO. If it returns different named objects with different execution paths, the compiler cannot perform the optimization.

:

, pragmas:

#pragma GCC push_options
#pragma GCC optimize (ARG)

//your code

#pragma GCC pop_options

, __ __ (()):

void __attribute__((optimize(ARG))) foo(unsigned char data) {
    // your code
}

ARG (.. ), , 0 (.. ) .. ARG "O0". , gcc -S, . gcc 4.4 .

+3

Source: https://habr.com/ru/post/1683995/


All Articles