A question on nature of generated assembly in C++ and code Algebra

Posted by Reetesh Mukul on Programmers See other posts from Programmers or by Reetesh Mukul
Published on 2012-04-26T19:49:21Z Indexed on 2012/09/15 9:50 UTC
Read the original article Hit count: 238

Filed under:
|

I wrote this code:

#include <iostream>
int main()
{
    int a;
    std::cin >> a;
    if(a*a== 3){
        std::cout << a;
    }
    return 0;
}

On MSVC I turned ON all optimization flags. I expected that since a*a can never be 3, so compiler should not generate code for the section:

if(a*a== 3){
    std::cout << a;
}

However it generated code for the section. I did not check GCC or LLVM/CLang.

What are the limits of expectation from a C++ compiler in these scenarios?

© Programmers or respective owner

Related posts about c++

Related posts about assembly