Consider this example:
extern void black_box_foo();
extern void black_box_bar();
int main(){
black_box_foo(); // #1
black_box_bar(); // #2
}
#1 and #2 are functions whose definitions are opaque to the implementations, for example, their definitions are in the static library.
[intro.abstract] p1 says:
In particular, they need not copy or emulate the structure of the abstract machine.Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.
And observable behaviors are listed in this exhaustive list([intro.abstract] p8)
The following specify the observable behavior of the program:
- Accesses through volatile glvalues are evaluated strictly according to the rules of the abstract machine.
- Data is delivered to the host environment to be written into files (See also: ISO/IEC 9899:2024, 7.23.3).
- The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.
All the above rules can be concluded as the "as-if" rule. Under this rule, implementations can reorder anything, as long as they provide the reordering doesn't change the observable behavior.
Without seeing the definitions of the functions, we cannot know whether these functions comprise the observable behavior specified in the above list. AFAIK, almost all major implementations take the conservative approach for black box functions: preserve the program order.
There are two questions:
Can conforming implementations eliminate the function call if these functions were written in other languages, for example, assembly, and do something that is not the observable behavior in terms of the C++ definition? If not, where are the relevant rules in the implementations' documents?
Can conforming implementations reorder
#1and#2when their definitions are opaque? If not, where are the relevant rules in the implementations' documents?