harmful things

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Platform
{
	cl_platform_id operator*() { return platform; }
	// no virtuals, no other data
protected:
	cl_platform_id platform;
};

std::vector<Platform> getPlatforms()
{
	vector<Platform> platforms;
	cl_uint n_platforms;
	clGetPlatformIDs(0, 0, &n_platforms);
	platforms.reserve(n_platforms);
	clGetPlatformIDs(n_platforms, reinterpret_cast<cl_platform_id*>(&platforms.front()), 0);
	return platforms;
}

This code fills vector platforms with n_platforms elements.
The only harmful until now, is that this code doesn't set the vector's size.
With this approach I avoid to create an intermediate array and then copy array data to vector.

Any comments on this trick?
Why don't you use resize instead of reserve?
Topic archived. No new replies allowed.