Optimizing away a “while(1);” in C++0x

前端 未结 8 1173
生来不讨喜
生来不讨喜 2020-11-22 10:37

Updated, see below!

I have heard and read that C++0x allows an compiler to print \"Hello\" for the following snippet

#include 

        
8条回答
  •  花落未央
    2020-11-22 10:51

    It is not decidable for the compiler for non-trivial cases if it is an infinite loop at all.

    In different cases, it can happen that your optimiser will reach a better complexity class for your code (e.g. it was O(n^2) and you get O(n) or O(1) after optimisation).

    So, to include such a rule that disallows removing an infinite loop into the C++ standard would make many optimisations impossible. And most people don't want this. I think this quite answers your question.


    Another thing: I never have seen any valid example where you need an infinite loop which does nothing.

    The one example I have heard about was an ugly hack that really should be solved otherwise: It was about embedded systems where the only way to trigger a reset was to freeze the device so that the watchdog restarts it automatically.

    If you know any valid/good example where you need an infinite loop which does nothing, please tell me.

提交回复
热议问题