Question: Consider the following code snippet:

```
List<String> list = new LinkedList<>();
list.add("Hello");
list.add("My");
list.add("Son");

for (String s: list){
    if (s.equals("My")) list.remove(s);
    System.out.printf("s=%s, list=%s\n",s,list.toString());
}

```

This results in output:

s=Hello, list=[Hello, My, Son]  

s=My, list=[Hello, Son]

So clearly the loop was only entered twice, and the third element, "Son", never gets visited. From the underlying library code, it looks like what happens is that the `hasNext()` method in the iterator doesn't check for concurrent modification, only the size against the next index. Since the size has been reduced by 1 by the `remove()` call, the loop simply doesn't get entered again, but no ConcurrentModificationException is thrown. 

This seems to contradict the contract of the iterator: 

> 
> The list-iterator is *fail-fast*: if the list is structurally modified at any time after the Iterator is created, in any way except through the list-iterator's own `remove` or `add` methods, the list-iterator will throw a `ConcurrentModificationException`. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
> 
> 
> 

Is this a bug? Again, the contract of the iterator definitely appears to be disobeyed here - the structure of the list is structurally modified by something other than the iterator in the middle of iteration.

Answer: 
See the C++11 Standard, §12.2/3, saying

> 
> Temporary objects are destroyed as the last step in evaluating the
>  **full-expression** (1.9) that (lexically) contains the point where they
>  were created.
> 
> 
>
What you are expecting to see is:

```
Construct 0x7fffa42d97ff         //  Creation of temporary object
Copy construct 0xAAAAAAAAAA      //  copy constuction of parameter
Inside f
Destruct 0xAAAAAAAAAA            // destruction of parameter.
Construct 0x7fffa42d97fe
Copy construct 0xBBBBBBBBB
Inside f
Destruct 0xBBBBBBBBB
Construct 0x7fffa42d97fd
Copy construct 0xCCCCCCCCC
Inside f
Destruct 0xCCCCCCCCC
Destruct 0x7fffa42d97fd
Destruct 0x7fffa42d97fe
Destruct 0x7fffa42d97ff          // destruction of temporary

```

But the compiler is allowed to elide (remove) the copy construction of parameters (and their destructors) and inline the function. If you do this the only remaining objects that are constructed are the temporaries that are passed to the functions.

So if you take my result set remove the copy construction (caused by aggressive compiler optimization) you are left with the output you see in your answer.

If you want to see the output above. Then prevent the compiler from inlining the functions: See <https://stackoverflow.com/a/1474050/14065>

Note: In-lining is just one reason for the eliding the copy. The compiler can use a couple of others. I use the example on in-lining because it is the most easy to visualize the removal of the parameter being copied into the function.