Short-circuited operators and tail recursion

前端 未结 1 1353
走了就别回头了
走了就别回头了 2021-01-12 17:23

Let\'s say I have a simple function like this:

int all_true(int* bools, int len) {
    if (len < 1) return TRUE;
    return *bools && all_true(boo         


        
1条回答
  •  有刺的猬
    2021-01-12 18:06

    Your question is really "how smart is the compiler?" but you don't state which compiler you are using.

    Given a hypothetical reasonable compiler which converts source code to an intermediary flow graph before optimizations, both fragments of code that you have written could be represented in the same way (the && operator, while convenient to type, is not nearly as trivially compiled as the & operator; so I wouldn't be surprised if it gets expanded out in one phase on a hypothetical compiler). On that assumption, it is reasonable to assert that the answer to your question is "yes".

    However, if you're actually going to rely on this, you should just test it with whatever compiler you happen to be using.

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