Detecting if a macro argument is a typename

删除回忆录丶 提交于 2019-12-10 12:53:04

问题


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 _Alignas 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.


回答1:


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.

  • If x is a type, () becomes the cast operator and +0 is the unary addition operator applied to an integer constant.
  • If 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)
  • If x is a non-zero integer constant expression, we get 1 + 0 + 1 = 2.
  • If x is a zero integer constant expression, we get 0 + 1 + 1 = 2.
  • If 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


来源:https://stackoverflow.com/questions/56149041/detecting-if-a-macro-argument-is-a-typename

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!