C++: Void versus [[Noreturn]](quuxplusone.github.io)
quuxplusone.github.io
C++: Void versus [[Noreturn]]
https://quuxplusone.github.io/blog/2022/06/29/that-undiscovered-country/
7 comments
I think Rust solved that pretty well, where no-return functions can be declared by letting it return the 'never' type:
https://doc.rust-lang.org/std/primitive.never.html
There is no way where you can say: this function returns a string and is no-return.
https://doc.rust-lang.org/std/primitive.never.html
There is no way where you can say: this function returns a string and is no-return.
It is different in other languages
FreePascal has the same issue with the warning in divide2 and it also has a noreturn attribute. So can you be tempted to add the attribute and it get rids of the warning. But it is wrong.
In FreePascal noreturn means that the function does not return, i.e. the execution does not leave the scope. An exception would leave the function, so it is not allowed.
Noreturn functions can only have an infinite loop or kill the process
FreePascal has the same issue with the warning in divide2 and it also has a noreturn attribute. So can you be tempted to add the attribute and it get rids of the warning. But it is wrong.
In FreePascal noreturn means that the function does not return, i.e. the execution does not leave the scope. An exception would leave the function, so it is not allowed.
Noreturn functions can only have an infinite loop or kill the process
I want to read a whole play about Strousencrantz and Guildenstrup now.
Nice post, but I believe that function g (with the infinite loop) is UB in C++, as it’s required that infinite loops that never break must have side-effects.
It isn't, because g's loop control is a constant expression. If it weren't, then it would be UB.
This is also why there's very little point to introspecting the presence of this annotation or even overloading based on this annotation.
Discussing whether a function which always terminates "returns" or "not-returns" an int is as useful as discussing whether a function which is never executed returns an int. The semantic analysis is rather clear in both cases.