I have the following variadic macro, which counts the number of arguments passed to it at compile time:
#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
For example, the following example is equivalent to 4
:
const int example = NUMARGS(3, 0, 2, 1); // = 4
I would like to do the same for array values:
const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} ); // = 3
Once this code is compiled, the variable is indeed set to 3
. Nevertheless, gcc displays the following warning:
main.c:10:5: warning: braces around scalar initializer
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^~~~~
main.c:10:5: note: (near initialization for ‘(anonymous)[0]’)
main.c:10:39: warning: excess elements in scalar initializer
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^
main.c:1:38: note: in definition of macro ‘NUMARGS’
23 | #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
| ^~~~~~~~~~~
main.c:10:39: note: (near initialization for ‘(anonymous)[0]’)
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^
main.c:1:38: note: in definition of macro ‘NUMARGS’
23 | #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
| ^~~~~~~~~~~
main.c:10:5: warning: braces around scalar initializer
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^~~~~
main.c:10:5: note: (near initialization for ‘(anonymous)[1]’)
main.c:10:47: warning: excess elements in scalar initializer
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^
main.c:1:38: note: in definition of macro ‘NUMARGS’
23 | #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
| ^~~~~~~~~~~
main.c:10:47: note: (near initialization for ‘(anonymous)[1]’)
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^
main.c:1:38: note: in definition of macro ‘NUMARGS’
23 | #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
| ^~~~~~~~~~~
main.c:10:5: warning: braces around scalar initializer
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^~~~~
main.c:10:5: note: (near initialization for ‘(anonymous)[2]’)
main.c:10:55: warning: excess elements in scalar initializer
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^
main.c:1:38: note: in definition of macro ‘NUMARGS’
23 | #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
| ^~~~~~~~~~~
main.c:10:55: note: (near initialization for ‘(anonymous)[2]’)
10 | const int example2 = NUMARGS( {3, 2}, {0, 1}, {4, 3} );
| ^
main.c:1:38: note: in definition of macro ‘NUMARGS’
23 | #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
| ^~~~~~~~~~~
gcc does not offer to disable this warning, and, if I'm not mistaken, it is not needed here as it is only used in my macro to count variadic arguments.
How can I make sure that the error is not displayed anymore, hoping not to make the writing more cumbersome in the use of the macro (as this is of course the aim)?