auto query


Hi,

When I replace auto& x with auto x at line 1 in the above, the code throws compilation error - saying that the thread is inaccessible. I do not understand why this works correctly at line 2. Can someone please explain? Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vector<thread> vecThreads;
	
	for (int i1=0;i1<10;i1++) {
		vecThreads.push_back(thread(incGlobal,1000));
	}

	for (auto& x : vecThreads) { // line 1
		x.join();
	}

	vector<int> vecInts;
	for (int i1=0;i1<10;i1++) {
		vecInts.push_back(i1);
	}
	for (auto x : vecInts) { // line 2
		cout << x << endl;
	}
If you do auto x x will be a copy of the element in the vector. Threads can't be copied and that is why you get an error.
Last edited on
thanks, Peter - got it
Topic archived. No new replies allowed.