Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

_Generic keyword support in C11

$
0
0

Hi,

The _Generic keyword is a C11 feature that does not seem available in Intel compilers. It would allow better implementation of object-oriented code in C. Do you plan to support it in the next releases ?

typedef struct {
	int size;
	double *data;
} VectorDouble;
typedef struct {
	int size;
	int *data;
} VectorInt;

#define alloc(a, n) _Generic((a), VectorDouble: alloc_VectorDouble(&a, n), VectorInt: alloc_VectorInt(&a, n))
#define release(a) _Generic((a), VectorDouble: release_VectorDouble(a), VectorInt: release_VectorInt(a))
#define at(a, i) _Generic((a), VectorDouble: a.data[i])

void alloc_VectorDouble(VectorDouble *a, int n) {
	a->data = (double*) malloc(n * sizeof(double));
}

void free_VectorDouble(VectorDouble a) {
	free(a.data);
}

void alloc_VectorInt(VectorInt *a, int n) {
	a->data = (int*) malloc(n * sizeof(int));
}

void free_VectorInt(VectorInt a) {
	free(a.data);
}
int main (int argc, char const *argv[])
{
	const int n = 8;

	VectorDouble a;
	VectorDouble b;
	VectorDouble c;

	alloc(a, n);
	alloc(b, n);
	alloc(c, n);

	for(int j = 0; j < n; ++j)
	{
		at(a, j) = at(b, j) + at(c, j);
	}

	release(a);
	release(b);
	release(c);

	return 0;
}

 

 

Best regards,

Francois


Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>