I'm trying to implement type-generic vector operations through C macros. My current implementation works if initialization and declaration happens on the same line, since the macro is then aware of the type of the vector:
vec2_t(char) char_vector = vec2(10, 20); // unexpanded
struct {char x, char y;} char_vector = {10, 20}; // expanded
However, the problem is that if I then try to reassign the vector, it's no longer aware of the type, and as a result it won't compile:
char_vector = vec2(20, 30); // unexpanded
char_vector = {20, 30}; // expanded
Expanding it to char_vector = (struct {char x, char y;}) {20, 30};
would fix the problem, but this would require having to specify the type everytime the vector is reassigned. The same issue also occurs with other vector operations such as adding, etc.
Is there a way to implement this without needing to specify the type on every reassignment? I realize typeof
could work here on Linux, but I'm looking for a platform-independent solution.