Document number: WG21/N2121 = J16/06-0191
Date: 2006-10-17
Revises: Library Active Issue 529
Project: Programming Language C++
Reference: N2009 = 06-0079
Reply to:
cpp@alantalbot.com
Tele Atlas North
11
After considerable discussion at the April 2006
The
version of the Standard used was N2009=06-0079, dated 2006-04-21. I identified
the instances of the problem by doing a text search on “Requires:”. I then examined each of the 164 instances for
consistency and redundancy with the “Throws:” clause (if present). I
also surveyed the cases where “Requires” was spelled “Precondition(s)”.
Issue 529 points out the following possible inconsistency in the Standard. 17.3.1.3 [lib.structure.specifications] states:
Requires: the preconditions for calling the function
17.4.3.8 [lib.res.on.required] further states:
Violation of the preconditions specified in a function’s Required behavior: paragraph results in undefined behavior unless the function’s Throws: paragraph specifies throwing an exception when the precondition is violated.
The argument is made in 529 that this is contradictory
because the definition of “precondition” is that it must be satisfied or
undefined behavior will result. Since throwing is defined behavior, this
creates a contradiction. 529 suggests removing the “unless” clause from this
paragraph. At the
There is also a confusing (and embarrassing) typo in this paragraph, namely that it refers to the “Required behavior:” paragraph, not the “Requires:” paragraph.
529 goes on to suggest removing redundant Requires
paragraphs in cases where the Requires paragraph does
not specify any conditions that are not responded to by the Throws paragraph.
There was wide consensus in
The order of paragraphs in the function specification sections varies from function to function. This could lead to misreading. I suggest this be corrected by making all such sections follow the order in section 17.3.1.3 [lib.structure.specifications]. I have listed those sections that differ from that order. (An alternative would be to swap the position of “Effects” and “Preconditions” in the list and then edit those places that differ from that order. This might make for fewer changes.)
I have proposed these changes below by stating “Reorder” in the Proposed Changes section, rather than by detailed change language.
The term “Requires” is potentially confusing (with the
“Required behavior” paragraph—note the typo in the current Standard), and is
asymmetrical with the “Postconditions” paragraph. I
suggest that the term “Preconditions” solves these problems. (Note this is
already used in some places! See: 22.2.1.4.2
[lib.locale.codecvt.virtuals].)
I propose a global change in which all instances of “Requires” as it pertains to function preconditions, and all instances of “Precondition”, be changed to “Preconditions”.
3 Descriptions of function semantics contain the
following elements (as appropriate):155)
— Requires: Preconditions:
the preconditions for
calling the function
— Effects: the actions performed by the function
— Postconditions: the observable results established by the function
— Returns: a description of the value(s) returned by the function
— Throws: any exceptions thrown by the function, and the conditions that
would cause the exception
— Complexity: the time and/or space complexity of the function
…
155) To save space, items that do not apply to a function are
omitted. For example, if a function does not specify any further preconditions,
there will be no “Requires” “Preconditions” paragraph.
Other possible issues:
This list does not include mention of the Replaceable: paragraph. Should it?
This list does not include mention of the Return type: paragraph. Should it?
17.4.3.8 Required Preconditions paragraph [lib.res.on.requiredpreconditions]
1 Violation of the preconditions
specified in a function’s Required behavior: Preconditions: paragraph results in
undefined behavior unless the function’s Throws:
paragraph specifies throwing an exception when one or more of the preconditions is are violated.
Reorder.
3 Requires:
pos <= str .size()
Reorder.
5 Requires:
n <= max_size()
Reorder.
2 Requires:
pos < size()
2 Requires:
pos <= str .size()
Reorder.
2 Requires:
pos <= str .size()
Reorder.
2 Requires:
pos1 <= size()
and pos2 <= str .size()
Reorder.
1 Requires:
pos <= size()
Reorder.
2 Requires:
pos1 <= size()
&& pos2 <= str .size().
Reorder.
1 Requires:
pos <= size()
Reorder.
Reorder.
1 Requires:
pos <= size()
Reorder.
Reorder.
3 Requires:
pos <= str .size().
Reorder.
13 Requires:
pos is
valid
19 Requires:
pos is
valid
27 Requires:
pos is
valid
42 Requires:
pos is
valid
Reorder.
Reorder.