Wednesday 14 October 2015

There is such a thing as too much compiler optimization

I spent my University years using antiquated subsets of C and C++, so I decided to try my hand at the latest Standards and Proposals.

After some time I found this weird... Bug? I had a GetNeighbours function that would fill out an array which was passed by reference. It spent a few days as an empty placeholder. Once I filled out the function so it would actually return the neighbours, I started getting compiler errors because std:array was undefined. How could this be? The function had been there for ages, and had always received an std:array parameter. Why was it only throwing out errors now?

The answer was simple but it took a bit of lateral thinking: The compiler had figured out GetNeighbours was performing no useful work and simply ignored it, not even caring that the type of argument was undefined. The function call had been removed and the code had never been compiled.

I'm ashamed to say this took me a few minutes to figure out.


This works
This doesn't (std::array is not a defined type)
This works. Wat?