Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the C++ standard, what is specified to occur when a "shall" requirement is violated?

For example, the famous words (§3.2/1)

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

I believe "shall" requirements are to be interpreted as though they are implicitly followed by "otherwise the program is ill-formed" unless otherwise specified. However, others claim that "shall" instead means "otherwise the behavior is undefined".

In every case I've come across in the standard in which a "shall" requirement was not followed by something like "otherwise the behavior is undefined" or "no diagnostic required", the rule it occurred in was one that is obviously diagnosable and is diagnosed by all compilers I know of (the above paragraph being an example). That's why I believe it means "otherwise the program is ill-formed", i.e., a diagnostic is required.

Anyway, those are just my thoughts. I'd appreciate an authoritative answer.

like image 241
Brian Bi Avatar asked Aug 14 '14 05:08

Brian Bi


2 Answers

Yes, to be well-formed, the program must follow the One Definition Rule that you've quoted in the question (§1.3.26):

well-formed program

C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule (3.2).

The other diagnosable rules are specified as (§1.4):

1.4 Implementation compliance [intro.compliance]

1 The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior.”
2 Although this International Standard states only requirements on C++ implementations, those requirements are often easier to understand if they are phrased as requirements on programs, parts of programs, or execution of programs. Such requirements have the following meaning:
— If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute2 that program.
If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this Standard as “conditionally-supported” when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.
— If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.
[emphasis added]

And yes, as noted in the second bullet point, if a diagnosable rule is violated, a diagnostic is required.

like image 50
Jerry Coffin Avatar answered Nov 11 '22 20:11

Jerry Coffin


In addition to @JerryCoffin's answer, there is also ISO/IEC Directives Part 2 (that governs all ISO/IEC documents, including the C++ Standard), in particular Annex H Verbal forms for the expression of provisions

The verbal forms shown in Table H.1 shall be used to indicate requirements strictly to be followed in order to conform to the document and from which no deviation is permitted.

shall:

  • is to,
  • is required to,
  • it is required that,
  • has to,
  • only … is permitted,
  • it is necessary

shall not:

  • is not allowed [permitted] [acceptable] [permissible],
  • is required to be not
  • is required that … be not
  • is not to be

So a violation of a "shall" requirement makes a program ill-formed. The diagnostic issues have been answered elsewhere.

like image 23
TemplateRex Avatar answered Nov 11 '22 18:11

TemplateRex