C++ CPA study materials
C++ CPA study resources

An observed output is not a language guarantee

C++ CPA · Put your knowledge to work in context.

All case studies

An observed output is not a language guarantee

A fictional learner evaluates two expressions in a C++ study exercise. The first divides two positive integers with a declared floating-point conversion before the division. The second modifies the same scalar object in two unsequenced operands of addition. A colleague runs both programs once and proposes recording the observed output as a guaranteed portable result for each. Another distinguishes type conversion, grouping, sequencing and undefined behavior, and asks for the stated language version and actual expression. This written case executes no supplied unknown program and changes no software. The learner must explain why one valid arithmetic trace can be calculated while a compiler’s incidental output cannot define behavior outside the language’s permitted rules.

1. Why is an observed run not sufficient for the second expression?

Read the worked answer

Unsequenced conflicting modifications can make the program undefined under the relevant rules. One implementation’s observed output does not create a portable guarantee.

2. What should the arithmetic trace document?

Read the worked answer

Record the actual types, conversion location, operands, language version and defined operation. Converting before division differs from converting an already truncated integer result.