Why is it impossible to build a compiler that can determine if a C++ function will change the value of a particular variable?

后端 未结 13 765
感动是毒
感动是毒 2021-01-30 02:01

I read this line in a book:

It is provably impossible to build a compiler that can actually determine whether or not a C++ function will change the val

13条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 02:43

    I think the key word in "whether or not a C++ function will change the value of a particular variable" is "will". It is certainly possible to build a compiler that checks whether or not a C++ function is allowed to change the value of a particular variable, you cannot say with certainty that the change is going to happen:

    void maybe(int& val) {
        cout << "Should I change value? [Y/N] >";
        string reply;
        cin >> reply;
        if (reply == "Y") {
            val = 42;
        }
    }
    

提交回复
热议问题