Related
- Syntax and Sample Usage of _Generic in C11
- _Generic works with typedef structs or only with primitive types?
- How do you use _Generic with structs that are typedef-ed in C?
Good day, everyone!
I am writing an macro that differentiate type of input struct. I am using MinGW GCC under Windows from WinLibs: https://winlibs.com/
An example from here: Syntax and Sample Usage of _Generic in C11 works great, and I was able to write same macro(Ex. A)
// Example A (Single Value):
#include <stdio.h>
#include <stdint.h>
//
typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
// any single value
#define deb_single(val) \
printf("[%s]: ", #val); \
_Generic( (val), \
default : puts("UNKNOWN_TYPE"), \
i32 : printf("[%d]" , (i32)val), \
u32 : printf("[%u]" , (u32)val), \
i64 : printf("[%lld]" , (i64)val), \
u64 : printf("[%llu]" , (u64)val) \
); \
printf("Func [%s], line [%d]\n", __func__, __LINE__);
i32 main()
{
i32 A = 111;
u32 B = 222;
i64 C = 333;
u64 D = 444;
deb_single(A);
deb_single(B);
deb_single(C);
deb_single(D);
return 0;
}
// Result_of Example A:
[A]: [111]Func [main], line [35]
[B]: [222]Func [main], line [36]
[C]: [333]Func [main], line [37]
[D]: [444]Func [main], line [38]
// Endof Example A.
Now, almost same approach (with typeof) works with structs of two same-type values (Ex. B)
// Example B (Struct of two same-type values):
#include <stdio.h>
#include <stdint.h>
//
typedef int32_t i32;
typedef uint32_t u32;
typedef float r32;
typedef double r64; // only for printf cast
typedef struct _tag_v2i { i32 x; i32 y; } v2i;
typedef struct _tag_v2u { u32 x; u32 y; } v2u;
typedef struct _tag_v2f { r32 x; r32 y; } v2f;
//
#define deb_struct(str) \
printf("[%s] @ line [%d]: ", #str, __LINE__); \
_Generic( (typeof(str)){0}, \
default : puts("UNKNOWN_TYPE"), \
struct _tag_v2i : printf("[%d][%d]\n", (i32)str.x, (i32)str.y), \
struct _tag_v2u : printf("[%u][%u]\n", (u32)str.x, (u32)str.y), \
struct _tag_v2f : printf("[%.2f][%.2f]\n", (r64)str.x, (r64)str.y) \
);
i32 main()
{
v2i vA = {111,999};
v2u vB = {222,888};
v2f vC = {1.5f, 5.25f};
deb_struct(vA);
deb_struct(vB);
deb_struct(vC);
return 0;
}
// Result_of Example B:
[vA] @ line [30]: [111][999]
[vB] @ line [31]: [222][888]
[vC] @ line [32]: [1.50][5.25]
// Endof Example B.
However, I struggle to mix single values and structs (Ex. C)
// Example C [/*ERRORS*/]:
#include <stdio.h>
#include <stdint.h>
//
typedef int32_t i32;
typedef struct _tag_v2i { i32 x; i32 y; } v2i;
//
#define deb_struct(str) \
printf("[%s] @ line [%d]: ", #str, __LINE__); \
_Generic( (typeof(str)){0}, \
default : puts("UNKNOWN_TYPE"), \
i32 : printf("[%d]" , (i32)str), \
struct _tag_v2i : printf("[%d][%d]\n", (i32)str.x, (i32)str.y) \
);
i32 main()
{
i32 sA = 555;
v2i vA = {111,999};
deb_struct(sA);
deb_struct(vA);
return 0;
}
// Result_of Example C:
error: request for member 'x' in something not a structure or union
14 | struct _tag_v2i : printf("[%d][%d]\n", (i32)str.x, (i32)str.y), \
error: request for member 'y' in something not a structure or union
14 | struct _tag_v2i : printf("[%d][%d]\n", (i32)str.x, (i32)str.y), \
error: aggregate value used where an integer was expected
23 | deb_struct(vA);
// Endof Example C.
So, my question is: how to make macro that takes as input single values AND struct (Ex. C)?
Thank you!