clang infinite tail recursion optimization

后端 未结 1 1691
情歌与酒
情歌与酒 2021-01-26 20:30
#include  

int foo(int i){ 
     return foo(i + 1);
} 

int main(int argc,char * argv[]){ 
     if(argc != 2){ 
         return 1; 
     } 
     std::co         


        
1条回答
  •  执笔经年
    2021-01-26 20:44

    While both g++ and clang++ are able to compile C++98 and C++11 code, clang++ was designed from the start as a C++11 compiler and has some C++11 behaviors embedded in its DNA (so to speak).

    With C++11 the C++ standard became thread aware, and that means that now there are some specific thread behavior. In particular 1.10/24 states:

    The implementation may assume that any thread will eventually do one of the following:

    — terminate,

    — make a call to a library I/O function,

    — access or modify a volatile object, or

    — perform a synchronization operation or an atomic operation.

    [Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. — end note ]

    And that is precisely what clang++ is doing when optimizing. It sees that the function has no side effects and removes it even if it does not terminate.

    0 讨论(0)
提交回复
热议问题