[clang] [Headers] Don't declare unreachable() from stddef.h in C++ (PR #86748)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 29 07:20:25 PDT 2024


AaronBallman wrote:

> > I can see WG21 solving this either by removing `unreachable` from `cstddef` or by requiring `utility` to guard against the macro via implementation magic (personally, I think `utility` should guard against it because of users including `stddef.h` directly) and it would be nice to know which direction the committee is leaning before we get too far into a solution.
> 
> I'm not sure whether you mean it, but I'd expect that it's handled like `signbit` and friends - they are functions instead of macros. For libc++ that would simply be `#undef unreachable`, `#include <__utility/unreachable.h>` and `using std::unreachable`.

Yeah, that would one way to handle it, but it does still defy some user expectations (and perhaps that's fine):
```
#include <stddef.h>
// #include "whatever.h" // transitively includes <utility>

#ifndef unreachable
#error "oh no, my macro!"
#endif
```
Uncommenting `whatever.h` suddenly causes an error which may be a surprise because some C standard library things remain macros even in C++ (like `offsetof`, or `va_start` and friends). Any C standard library function can be implemented as a macro, so the potential for confusion always exists, but for things defined explicitly as a macro in C, they sometimes stay a macro in C++. I think `unreachable` is probably reasonable to turn into a real function though.

But another way WG21 could perhaps approach this is:
```
// In <utility>
#ifndef unreachable
void unreachable();
#endif
```
which retains the macro rather than the function.

https://github.com/llvm/llvm-project/pull/86748


More information about the cfe-commits mailing list