9

I was replying to a comment on my answer: C job interview - casting and comparing and found that I could not find a complete list of what C++ deems "Implementation-Defined behavior." I'm aware there are 3 categories for this sort of thing: Undefined behavior, Implementation-Defined behavior, and Unspecified behavior; however it seems most discussions center around Undefined behavior, and when Implementation-Defined behavior is discussed, at most one example of it is given. In general I tend to write a lot of code that traipses into this area and I know what sort of behavior to expect; still I would like to be able to knowledgeably be able to comment on its validity. I would also like to throw out there that I think there is a lot of misdiagnoses in the community of operations being undefined, when really they are well defined by the platform.

Please note that I am not so much interested in how a given platform chooses to define such behavior, but rather to have a list of all behavior that falls into the “Implementation-Defined” category as defined by the C++ standard.

Community
  • 1
  • 1
Apriori
  • 2,308
  • 15
  • 16

1 Answers1

11

The standard itself defines all the cases of implementation defined behavior, the draft C++ standard has an Index of implementation-defined behavior at the end which provides topics and which page the topic is addressed on, for example:

additional formats for time_get::do_get_date, 689

alignment, 76

alignment additional values, 76

In fact each compiler should document all the implementation defined behaviors and how it deals with them. For example here is gcc's C++ Implementation-defined behavior section and gcc's C Implementation-defined behavior. As far as I know the C++ standard does not unlike the C99 draft standard provide a reference for unspecified or undefined behavior. So you will have to resort to searching in the document for undefined and unspecified to find all instances.

The C99 draft standard provides a reference for unspecified, undefined, and implementation defined behavior in Annex J.1, J.2 and J.3 respectively.

It is important to note that undefined behavior can be specified as well defined by an implementation but they do have to document it. In your specific example, it does look like you are violating strict aliasing rules but your alternative suggestion to use type-punning is well defined since C89.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740