The Point Of No Return

As part of learning the new feature set in C++11, I stumbled upon the weird syntax for the new “attribute” feature: [[ ]]. One of these new C++11 attributes is [[noreturn]].

The [[noreturn]] attribute tells the compiler that the function you are writing will not return controlof execution back to its caller – ever. For example, this function will generate a compiler warning or error because even though it returns no value, it does return execution control back to its caller:

The following functions, however, will compile cleanly:

Using [[noreturn]] enables compiler writers to perform optimizations that would otherwise not be available without the attribute. For example, it can silently (but correctly) eliminate the unreachable call to fnReturns() in this snippet:

Note: I used the Coliru interactive online C++ IDE to experiment with the [[noreturn]] attribute. It’s perfect for running quick and dirty learning experiments like this.

Like this:

Related

`fnForever` isn’t allowed anyway. As of c++11 `while(true);` is ill formed, due to (the first point of) 1.10/24:
The implementation may assume that any thread will eventually do one of the following:
— terminate,
— make a call to a library I/O function,
— access or modify a volatile object, or
— perform a synchronization operation or an atomic operation.

Yes, gcc 4.8.1 is c++11 complete (modulo bugs and standard library). And even if you never create a `std::thread`, or anything else like that, you will always have at least one thread running ( otherwise where is `main()` running? ). This is just one of those odd things the compiler can assume while optimising. It doesn’t have to warn about it, and in fact in general it might not be able to, but in this case it is well within its rights to just remove the `while(true);` entirely. `for(;;);` is no better.
There’s some discussion here: http://stackoverflow.com/q/3592557/1171191

On P372, Ch13 (Exception Handling) of the 4th edition of TCPPL, Stroustrup uses it in an example to demonstrate program termination where exception handling (for some project or regulatory reason) may not be allowed. Interestingly, “The C++ Primer” 5th edition doesn’t even mention [[noreturn]]. Neither does the second edition of “The C++ Standard Library”.

“This attribute is useful for a few library functions such as abort and exit which cannot return. The user can also define their own functions that never return using this attribute.
The noreturn keyword tells the compiler to assume that fatal cannot return. It can then optimize without regard to what would happen if fatal ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables. You cannot assume that registers saved by the calling function are restored before calling the noreturn function. It does not make sense for a noreturn function to have a return type other than void. This is a good attribute because it gives additional information that can be
used by the optimizer, but does not alter the semantics of the program if removed.”