`[[reproducible]]` is a C23 function attribute. It promises consistent results and effects for given arguments while allowing limited temporary state changes. The promise is the programmer’s responsibility: the compiler may use it to optimize calls, but the attribute does not make a function reproducible. section 16.0 section 16.3
Pointers make “given arguments” more than a comparison of pointer values. Objects visible through pointer parameters or returns can matter to a call. Check those objects and any relevant execution state before relying on repeated calls to behave alike. section 10.2.3 section 16.3
#include <math.h>
#include <fenv.h>
inline double distance (double const x[restrict static 2])
[[reproducible]] {
# pragma FP_CONTRACT OFF
# pragma FENV_ROUND FE_TONEAREST
// We assert that sqrt will not be called with invalid arguments
// and the result only depends on the argument value.
extern double sqrt(double) [[unsequenced]];
return sqrt(x[0]*x[0] + x[1]*x[1]);
}
The `distance` definition carries `[[reproducible]]`. Its floating-point pragmas and annotated `sqrt` declaration show why checking an apparent function of its arguments requires attention to the floating-point environment and called functions. section 16.3
double g (double y[static 1], double const x[static 2]) {
// We assert that distance will not see different states of the floating
// point environment.
extern double distance (double const x[restrict static 2])
[[unsequenced]];
y[0] = distance(x);
...
return distance(x); // Replacement by y[0] is valid.
}
The caller makes an additional, local `[[unsequenced]]` assertion about `distance`, explicitly assuming it will not observe different floating-point states. Under that assertion, it can replace the second call with the saved result. section 16.3
TAKEAWAY: Treat `[[reproducible]]` as a checked optimization promise, not a request to trade correctness for speed. Its potential performance benefit still calls for measurement. section 16.0 section 16.5
