In Visual C++ 2013, it is legal to assign a temporary value to a const reference, for example
const int& a = 1;
In a large software project, I now stumbled upon a single line of code that took around 10 ms to execute:
const std::vector<Item>& desc = (hasItems) ? instance->getItems() : std::vector<Item>();
Item is a struct with around 20 bytes, instance->getItems() returns a const std::vector< Item>&. I did not see any reason why this line should take longer than an instant to execute, so I played around a bit and found out that
const std::vector<Item>& desc = std::vector<Item>();
also takes 10 ms, while
std::vector<Item> items;
const std::vector<Item>& desc = items;
is near-instant, i.e. 0.00.. ms, as you would expect.
The thing is, I can reliably reproduce this issue in the existing, complex software project, but in any condensed minimal example, both versions run equally fast. I have tested all of this in a release build with optimizations enabled. Is there any chance that one of these optimization makes this exception to assign a value to a const reference really slow? I would really like to know what causes this behavior. Does anyone have a clue what causes this?
Following up, is there a warning for assigning a value to a const reference that I accidentally disabled? If so, what is its id? It would be handy if the compiler could point out other places in the code where this happens.
Edit: The timings were done with a simple CPU clock right before and right after the code of interest, divided by the clock cycles per second. It's not too accurate, but gives you a general idea of the difference between 10.something and 0.something. And it's not fluctuating in any way, it is perfectly reproducible in this specific example. There is a significant difference in execution time.