Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's more useful in C++. Imagine a templated vector destructor:

    for(T* ptr = begin; ptr != end; ++ptr)
    {  destroy(ptr); }
For many types, 'destroy' will be empty (for example integers), so this just turns into:

    for(T* ptr = begin; ptr != end; ++ptr)
    { }
Which is an empty loop and can be optimised away -- assuming we can prove it isn't an infinite loop! Which can be quite hard sometimes (in this case, it needs that 'end' is some multiple of sizeof(T) bigger than begin).

Now in many cases you can prove these loops are finite, but in full generality it's quite hard, and it was considered easier to just let the compiler remove them.



Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: