Within C11/gnuC11 is it possible to write a macro that returns an integer constant expression of value 1 or 0 respectively if the macro argument is or isn't a type name or at least a macro can distinguish between integer constant expressions and typenames (i.e., if can detect the argument isn't one of these, it can assume it is the other)?
#define IS_TYPENAME(X) /*???*/
_Static_assert( IS_TYPENAME(int), "" );
_Static_assert( !IS_TYPENAME(42), "" );
Motivation:
My motivation was to wrap _Aligna
s with a macro that would simply do nothing if the suggested alignment (either type or integer expression) was less than the current one (normal _Alignas
with a smaller alignment results in an error) and so I wanted to also accept either a typename or an integer expr, but now I'm thinking simply requiring an integer expr (which you can always get from a typename by applying _Alignof
) will be the simpler/clearer way to go.
No. Macros in C are inherently type-unsafe and trying to check for types in C is fraught with problems. First, macros are expanded by textual substitution in a phase of compilation where no type information is available. For that reason, it is utterly impossible for the compiler to check the type of the arguments when it does macro expansion.
This article will demonstrate the use of the VBA TypeName Function. The VBA TypeName Function is used in determining the type of data stored in a cell, or the type of a selected object – for example a worksheet, range or cell, or a control on a form. To determine was datatype in in a cell we can use the TypeName function with the Cells Property.
For that reason, it is utterly impossible for the compiler to check the type of the arguments when it does macro expansion. Secondly, when you try to perform the check in the expanded code, like the assert in the question, your check is deferred to runtime and will also trigger on seemingly harmless constructs like
No. Macros in C are inherently type-unsafe and trying to check for types in C is fraught with problems. First, macros are expanded by textual substitution in a phase of compilation where no type information is available.
In order to do this, you will need to check if the parameter is of type integer, and you need to check if it is a type or an expression.
Checking if a macro parameter, that may be a type or an expression, is of integer type:
This can be done with _Generic
. A _Generic
expression cannot contain two types that are identical, so it should suffice if you compare against all the stdint.h types only. Since these will alias with the default integer types, but not collide with each other (like for example int
and long
might).
Now _Generic
doesn't accept a type as operand, so you have to tweak the input to always become an expression.
The trick that I invented just now, is to use the ambiguity between the parenthesis operator and the cast operator, and at the same time use the ambiguity between unary + and binary + operators.
Given (x)+0
.
x
is a type, ()
becomes the cast operator and +0
is the unary addition operator applied to an integer constant.x
is an expression, it will get parenthesized and then +
is the binary addition operator. So you can do:
#define IS_INT(x) _Generic((x)+0, \
uint8_t: 1, int8_t: 1, \
uint16_t: 1, int16_t: 1, \
uint32_t: 1, int32_t: 1, \
uint64_t: 1, int64_t: 1, \
default: 0)
This will work for all integer, character and float types, as well as pointers. It will not work on struct/union types (compiler error). It will not work with void*
and probably not with NULL
(compiler error, can't do pointer arithmetic).
Checking if a macro parameter, that may be a type or an expression, is an expression:
This can also be done using the same trick as above, use the ambiguity between different operators. For example:
#define IS_EXPR(x) (!!(x) + !(x) + 1 == 2)
x
is a non-zero integer constant expression, we get 1 + 0 + 1 = 2
.x
is a zero integer constant expression, we get 0 + 1 + 1 = 2
.x
is a type, we get !!(int)+!(int)+1
which equals 0
. Both + are unary.This doesn't make a difference between float and integers though, so we need to combine this trick with the IS_INT
macro.
Solution:
#define IS_INTCONSTEXPR(x) ( IS_INT(x) && IS_EXPR(x) )
Complete example with test cases, printing 1 if integer constant expression, otherwise 0:
#include <stdint.h>
#include <stdio.h>
#define IS_INT(x) _Generic((x)+0, \
uint8_t: 1, int8_t: 1, \
uint16_t: 1, int16_t: 1, \
uint32_t: 1, int32_t: 1, \
uint64_t: 1, int64_t: 1, \
default: 0)
#define IS_EXPR(x) (!!(x) + !(x) + 1 == 2)
#define IS_INTCONSTEXPR(x) ( IS_INT(x) && IS_EXPR(x) )
#define test(arg) printf("%d %s\n", IS_INTCONSTEXPR(arg),(#arg))
int main (void)
{
test(42);
test(sizeof(int));
test(1+1);
test(int);
test(unsigned int);
test(42.0);
test(double);
test(uint32_t);
test(uint32_t*);
test(_Bool);
_Static_assert( !IS_INTCONSTEXPR(int), "" ); // OK, passed
_Static_assert( IS_INTCONSTEXPR(42), "" ); // OK, passed
return 0;
}
Output:
1 42
1 sizeof(int)
1 1+1
0 int
0 unsigned int
0 42.0
0 double
0 uint32_t
0 uint32_t*
0 _Bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With