6 ms·
I was only really aware of undefined behavior that was relevant to runtime behavior. I wasn't aware using compiler reserved identifiers was undefined behavior;
by thethirdone 5y ago
I was only really aware of undefined behavior that was relevant to runtime behavior. I wasn't aware using compiler reserved identifiers was undefined behavior; I assumed it was required to error.
I don't see the purpose in allowing trivially detectable UB to exist. In this case and in the case of reserved identifiers, I don't see how any optimization could be made using the UB. Noticing the UB allows you to do anything, so you might as well rewrite the program to do nothing.
- account42 5y ago> I assumed it was required to error. They are reserved because the compiler (and OS) writers need identifiers they can use in their own headers without breaking valid user code. Since there is no good way for the compiler to say which headers are user headers, erroring on those symbols would be counter-productive.
- Kranar 5y agoC++ has a lot of these silly rules. For the case of reserved identifiers, they are reserved for the implementation of the standard library, but of course C++ uses a copy and paste system of #include's which takes place in the preprocessing phase, before the actual parsing/compiling phase. That means when you #include <vector> into your own main.cpp, you're copying and pasting "vector" into "main.cpp" and compiling "main.cpp", so the compiler would have to jump through all kinds of hoops to figure out that a certain section of "main.cpp" comes from "vector" and another section comes from this or that. It's certainly possible to do this in principle, but the standard does not impose that burden on any implementation. At any rate, the standard is full of this kind of compile time "undefined behavior". The formal term for this is "ill-formed, no diagnostic required" aka "NDR" which means that the program is invalid C++ but the compiler is not required to report an error, and running the subsequent translation is undefined behavior.