63

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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
cpp_noname
  • 2,031
  • 3
  • 17
  • 30
  • 2
    Give an example of code, and why you don't want this kind of optimization – fge Jan 06 '12 at 12:59
  • 2
    @fge I am debugging and testing the implementation of my software library so I would prefer to see every single step throughout the course of the execution of my software... – cpp_noname Jan 06 '12 at 13:06
  • 6
    Of course, you do realize that many people will actually use your library with constructor elision, so you need to test it in that configuration too ? – Matthieu M. Jan 06 '12 at 13:11
  • 4
    @Matthieu M, thanks for your reminding. – cpp_noname Jan 06 '12 at 13:14

1 Answers1

109

-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]


Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 2
    Off topic: it might save someone some time to note that clang [my --version is Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)] has a bug when using this flag, which in my case seems to produce seg faults : http://llvm.org/bugs/show_bug.cgi?id=12208 – Patrick Sanan Feb 10 '14 at 18:33