When dealing with T
instances obtained from an array or trough any other longish syntax, I often use a const T&
to alias the object and make my code more readable (of course only if the lifetime of the object allows it). I've seen this elsewhere, e.g. here on Stefan Reinalter's excellent blog. Stripped down and commented version of his code:
void Render()
{
for (size_t i = 0; i < m_visibleSubMeshes.size(); ++i)
{
// Get current submesh from array and create alias
const SubMesh& sm = m_subMeshes[i];
// Enjoy shortened syntax using the const reference
context->Draw(sm.startIndex, sm.numIndices);
}
}
Does this ever result in additional instructions, or will this under all circumstances be the same as accessing m_subMeshes[i].startIndex
and m_subMeshes[i].numIndices
directly?
It depends on the configuration.
If optimizations are in place it should produce the same results for any good compiler. This would be typical for release builds.
If optimizations are disabled it should produce fewer instructions, since you are de-referencing the collection just one time. This would be typical for debug builds, in this case it could also have the advantage of easier object inspection.