Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling g++'s return-value optimisation

What flag(s) do I need on the command line to disable the return-value optimisation automatically enabled by the g++ compiler?

like image 356
cpp_noname Avatar asked Jan 06 '12 12:01

cpp_noname


People also ask

How do I turn off return value optimization?

Named RVO GCC performs NRVO by default, but it can be disabled using the -fno-elide-constructors + compiler option. In contrast, MSVC disables NRVO by default, but it can be enabled using /O2 optimization+.

How does return value optimization work?

In the context of the C++ programming language, return value optimization (RVO) is a compiler optimization that involves eliminating the temporary object created to hold a function's return value. RVO is allowed to change the observable behaviour of the resulting program by the C++ standard.

Does C have return value optimization?

> Note also that C doesn't have return-value-optimization, hence all your struct-returning functions will cause a call to memcpy (won't happen when compiled in C++ mode of course).


Video Answer


1 Answers

-fno-elide-constructors

The C++ standard allows an implementation to omit creating a temporary which is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases. [Source: man gcc]


like image 185
Prasoon Saurav Avatar answered Oct 05 '22 23:10

Prasoon Saurav