I happened to stumble across this piece of code.
int x(int a){
std::cout<<a<<std::endl;
return a + 1;
}
int main()
{
std::cout<<sizeof(x(20))<<std::endl;
return 0;
}
I expected it to print 20 followed 4. But it just prints 4. Why does it happen so? Isn't it incorrect to optimize out a function, that has a side effect (printing to IO/file etc)?
sizeof
is a compile-time operator, and the operand is never evaluated.
sizeof
is actually an operator and it is evaluated in compile-time.
The compiler can evaluate it because the size of the return type of x
is fixed; it cannot change during program execution.
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