3 ms·
C++: Void versus [[Noreturn]]
- josephcsible 4y agoI want to read a whole play about Strousencrantz and Guildenstrup now.
- AshamedCaptain 4y agoSeemingly the confusion is due to the multiple meanings of the word "return" here? One can visualize the annotation as [[aborts]] or [[terminates]], rather than "doesn't return". 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.
- benibela 4y agoIt 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
- basicoperation 4y agoNice 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.
- josephcsible 4y agoIt isn't, because g's loop control is a constant expression. If it weren't, then it would be UB.
- EdSchouten 4y agoI 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 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.
- hn1337 4y ago