Meaning of auto& :

前端 未结 1 1166
野趣味
野趣味 2021-02-01 07:46

I understand that auto means type deduction. I\'ve never seen it used as auto& and furthermore I don\'t understand what : is doing in this short c

相关标签:
1条回答
  • 2021-02-01 08:24

    You are almost correct with your sample code.

    Auto meaning was redefined in C++11. The compiler will inferer the right type of the variable that is being used.

    The syntax with : it's a range based for. It means that loop will parse each element inside threads vector.

    Inside the for, you need to specify the alias auto& in order to avoid creating a copy of the elements inside the vector within the thread variable. In this way every operation done on the thread var is done on the element inside the threads vector. Moreover, in a range-based for, you always want to use a reference & for performance reasons.

    0 讨论(0)
提交回复
热议问题