Years ago I believed that C was absolutely pure compared to C++ because the compiler couldn't generate any code that you couldn't predict. I now believe counter examples include the volatile
keyword and memory barriers (in multiprocessor programming or device drivers for memory-mapped hardware devices, where plain assembly language would be even more pure than the optimizations of a C compiler).
At the moment I'm trying to enumerate the unpredictable things a C++ compiler can do. The main complaint that sticks in my mind about C++ is that the compiler will implicitly instantiate temporary objects, but I believe these cases can all be expected. The cases I'm thinking of are:
- when a class defines a copy constructor for a type other than itself, without using the
explicit
keyword - when a class defines an overloaded conversion operator:
operator ()
- when a function accepts an object by value instead of by reference
- when a function returns an object by value instead of by reference
Are there any others?