Say I have the following class:
class A{ };
And then in my code I have a function:
A foo(){
A ret;
//Do stuff
return ret;
}
And then I use the function later....
Will an optimizing compiler (like g++
) just treat foo()
like a void
function and skip actually allocating memory for the empty object? It might not do this because even an empty class has a size of 1.
This is a use case for gcc.godbolt.org where you can see what assembler code is generated (I recommend you click the colourize button to see which C++ code corresponds to which assembler code). You can see that even with -O0 there is no code generated to allocate or copy the object. You can try with other compilers and optimization levels. You can use #define
s to easily compare the code between returning class A
and void
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With