Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a complete list of Implementation-Defined behavior in C++?

Tags:

c++

standards

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.

like image 375
Apriori Avatar asked Jan 20 '14 03:01

Apriori


People also ask

What is implementation defined Behaviour in C?

11.1 Implementation-defined behavior. This is how CPP behaves in all the cases which the C standard describes as implementation-defined . This term means that the implementation is free to do what it likes, but must document its choice and stick to it.

What is the difference between unspecified and undefined in C?

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. Below is the program to illustrate Unspecified Behaviour: C.


1 Answers

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.

like image 139
Shafik Yaghmour Avatar answered Sep 25 '22 03:09

Shafik Yaghmour