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 code.
#include <iostream>
#include <vector>
#include <thread>
void PrintMe() {
std::cout << "Hello from thread: " << std::this_thread::get_id() << std::endl;
}
int main() {
std::vector<std::thread> threads;
for(unsigned int i = 0; i < 5; i++) {
threads.push_back(std::thread(PrintMe));
}
for(auto& thread : threads) {
thread.join();
}
return 0;
}
I can guess this is some sort of syntatic sugar that replaces
for(std::vector<std::thread>::iterator it = threads.begin(); it != threads.end(); it++ ) {
(*it).join();
}
but I don't understand how this syntax works and what that & sign is doing there.
(*)operator! Should've been written asit->join();...