Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ Standard 1.9/5 talk about "possible execution sequences"?

Tags:

c++

According to C++03 Standard 1.9/5

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input.

I don't get the "as one of" part.

If I have a specific program and a specific input and my program doesn't contain undefined behavior why would observable behavior vary? What is meant under "one of the possible execution sequences"?

like image 205
sharptooth Avatar asked Jul 19 '12 13:07

sharptooth


2 Answers

In C++, certain things are left up to the implementation. For example, when you write

int x = f(a) + f(b);

The implementation may choose to call f(a) first or f(b) first.

like image 154
Vaughn Cato Avatar answered Oct 05 '22 23:10

Vaughn Cato


Consider:

x = f() + g();

This allows two possible execution sequences:

__temp1 = f();           /*or*/     __temp1 = g();
__temp2 = g();           /*or*/     __temp2 = f();
x = __temp1 + __temp2;   /*or*/     x = __temp2 + __temp1;

The standard does not specify which of these must be performed; just that the program must behave as if one of these two were performed. If f() and g() have side effects, then the program could have one of two different observable behaviours.

like image 35
Mike Seymour Avatar answered Oct 06 '22 01:10

Mike Seymour