This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

small problem with auto (c++0x)


Hello, please consider the little program below which was compiled on GCC 4.7.0.

At the line containing the comment /* auto */, when using auto instead of vector<int>, the expected result which would be [3][6][9][12][15] is computed as [6][12][15][15][15] for some reason. Despite the high chance of the fault probably being mine, I think it'd be interesting either-way.

#include <thread>
#include <iostream>
#include <vector>

using namespace std;

#define NUM_OF_THREADS 5

int data[NUM_OF_THREADS] = {0};

void add(int* dest, const vector<int>& arr) {
	*dest = 0;
	for ( const auto& x : arr ) {
		*dest += x;
	}
}

int main(void) {
	vector<thread> threads;
	// data[i] := i + i+1 + i+2
	for ( int i=0; i<NUM_OF_THREADS; ++i ) {
/*		auto */ vector<int> arr = {i,i+1,i+2};
		threads.push_back( thread{add,data+i,arr} );
	}

	for ( auto& thread : threads ) {
		thread.join();
	}

	for ( const auto& dat : data ) {
		cout << "[" << dat << "]";
	} cout << endl;

	return 0;
}

Amit Markel


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]