Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it undefined behavior to #define/#undef an identifier with special meaning?

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?

like image 506
Shafik Yaghmour Avatar asked May 18 '15 19:05

Shafik Yaghmour


People also ask

What causes undefined behavior?

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.

What happens undefined behavior?

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.

Is unspecified behavior undefined behavior?

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.

Is an infinite loop undefined behavior?

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.


1 Answers

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.

like image 73
Shafik Yaghmour Avatar answered Oct 19 '22 22:10

Shafik Yaghmour