我对编译器知之甚少,但知道它们很复杂,足够聪明,可以优化代码.假设我的代码看起来像这样:
string foo = "bar";
for(int i = 0; i < foo.length(); i++){
//some code that does not modify the length of foo
}
Run Code Online (Sandbox Code Playgroud)
GNU编译器是否足够聪明,可以意识到在foo此循环过程中长度不会改变并用foo.length()适当的值替换调用?或者会foo.length()被称为每次i比较?