wrazhevsky
wrazhevsky

Reputation: 1

Get error when combine two _Generic

I want to combine two types in C for calling function (like multiply vector and matrix with different columns and rows):

#define CC_FIRST(a)        _Generic((a), int: int8d)
#define CC_SECOND(b)       _Generic((b), int: int16d)
#define CC_SP(first, second) first ## second
#define TEST(a,b) CC_SP(CC_FIRST(a), CC_FIRST(b)) (a,b)

int test1 = 10;
int test2 = 25;

TEST(10,25); // => int8dint16d(10,25), but not work

And have this:

pasting ")" and "CC_FIRST" does not give a valid preprocessing token #define TEST(a,b) CC_SP(CC_FIRST(a), CC_FIRST(b)) (a,b)

expected identifier or '(' before '_Generic' #define CC_FIRST(a) _Generic((a), int: int8d)

pasting ")" and "CC_FIRST" does not give a valid preprocessing token #define TEST(a,b) CC_SP(CC_FIRST(a), CC_FIRST(b)) (a,b)

'int8d' undeclared (first use in this function) #define CC_FIRST(a) _Generic((a), int: int8d)

expected ';' before '_Generic' #define CC_FIRST(a) _Generic((a), int: int8d) ^

What I am doing wrong?

Upvotes: 0

Views: 115

Answers (1)

Jens Gustedt
Jens Gustedt

Reputation: 78903

_Generic is not at all what you seem to expect. In particular it is not evaluated by the preprocessor. The preprocessor knows nothing about types, but only about textual tokens. So concatenating _Generic and () of the previous part can never work, because they wouldn't form a valid token.

Upvotes: 1

Related Questions