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

后端 未结 13 734
感动是毒
感动是毒 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:48

    It is impossible in general to for the compiler to determine if the variable will be changed, as have been pointed out.

    When checking const-ness, the question of interest seems to be if the variable can be changed by a function. Even this is hard in languages that support pointers. You can't control what other code does with a pointer, it could even be read from an external source (though unlikely). In languages that restrict access to memory, these types of guarantees can be possible and allows for more aggressive optimization than C++ does.

提交回复
热议问题