An answer to the question Disable check for override in gcc suggested using -Doverride=
on the command line to disable errors for erroneous use of override, which is effectively the same as adding:
#define override
to the source file.
My initial reaction was that this seems like undefined behavior since we are redefining a keyword but looking at the draft C++11 standard section 2.12
Keywords [lex.key] I was surprised that neither override nor final are keywords. They are covered in the previous section 2.11
[lex.name] which says they are identifiers with special meaning:
The identifiers in Table 3 have a special meaning when appearing in a certain context[...]
and Table 3 is labelled Identifiers with special meaning and includes both override and final.
The question is, is it undefined behavior to redefine(using #define) identifiers with special meaning? Are they treated any differently than keywords in this respect?
Whenever the result of an executing program is unpredictable, it is said to have undefined behavior. As a C programmer, understanding undefined behavior is very important for optimal coding and for the program to yield a good efficiency, especially when it comes to there are C codes embedded in system design.
Undefined behavior can lead to security vulnerabilities in software. For example, buffer overflows and other security vulnerabilities in the major web browsers are due to undefined behavior. The Year 2038 problem is another example due to signed integer overflow.
Undefined Behavior results in unpredicted behavior of the entire program. But in unspecified behavior, the program makes choice at a particular junction and continue as usual like originally function executes.
An infinite loop is well defined behavior. Transferring execution to a function and returning is well defined behavior. The compiler can only determine the behavior of the loop based on the declaration of the function.
If you are using the C++ standard library it is undefined behavior to redefine identifiers with special meaning, this also applies to keywords. From the draft C++11 standard under section 17.6.4
[constraints] we have section 17.6.4.1
[constraints.overview] which says:
This section describes restrictions on C++ programs that use the facilities of the C++ standard library [...]
and under 17.6.4
we have section 17.6.4.3.1
[macro.names] which says:
A translation unit shall not #define or #undef names lexically identical to keywords, to the identifiers listed in Table 3, or to the attribute-tokens described in 7.6.
Table 3 list the Identifiers with special meaning. We can see this paragraph also covers keywords and they are treated in the same manner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With