lundi 31 août 2020

Will compiler unroll "for" loop when iterating a const container?

In C++11, we can use a much simpler "for" loop when iterating a container like the following:

for (auto i : {1, 2, 3, 4})
    ...;

However, I don't know the efficiency of such code. Specifically:

  • What is the type of {1, 2, 3, 4}? It is an raw array, or converted to other containers such as std::vector?
  • Will compiler unroll the loop?

Update: Suppose we are using -O2, and the codes in the loop are only a few operations. As my case, I want to enumarate four directions UP DOWN LEFT RIGHT and call a function with the direction parameter. I just care if the program can have the best performance.

Thank you very much!

Aucun commentaire:

Enregistrer un commentaire