-1

Is there a compiler attributes (clang / gcc) that does something like noreturn but for loop?

Something like:

__atribute__((loopsnoreturn)) for (int i=0; i<12; i++)
{
    if(i==5) return 0; //< Compiler error
}
2
  • noreturn means a function cannot even flow off the end to an implicit return. What should happen when the loop just ends in your example? Commented Aug 17 at 20:55
  • Seems like a typo in for (int i=0; i<12; <i++). I'd just fix it, but not sure I understand the question enough to be confident. Commented Aug 17 at 22:57

1 Answer 1

3

If the loop can only exit via return, then the loop condition i<12 must be redundant and you can simplify it to just 1. That should be enough for the compiler to realize that code after the loop is unreachable and have the optimization effects that you seem to expect.

Otherwise, you can also use __builtin_unreachable(); after the loop in order to the tell the compiler that the path after the loop can't be reached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.