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

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

    Don't confuse "will or will not modify a variable given these inputs" for "has an execution path which modifies a variable."

    The former is called opaque predicate determination, and is trivially impossible to decide - aside from reduction from the halting problem, you could just point out the inputs might come from an unknown source (eg. the user). This is true of all languages, not just C++.

    The latter statement, however, can be determined by looking at the parse tree, which is something that all optimizing compilers do. The reason they do is that pure functions (and referentially transparent functions, for some definition of referentially transparent) have all sorts of nice optimizations that can be applied, like being easily inlinable or having their values determined at compile-time; but to know if a function is pure, we need to know if it can ever modify a variable.

    So, what appears to be a surprising statement about C++ is actually a trivial statement about all languages.

提交回复
热议问题