在C编译时检查类型是结构还是指针? [英] Checking if a type is a struct or pointer at compile time in C?
问题描述
注意:这不是Paul T所链接问题的重复,因为我在问是否有可能在编译时确定某个类型是否具有某种更广泛的不完整类型/种类,如果在编译时已注册符号,则不会。这似乎是对该问题的根本误会。
NOTE: This is NOT a duplicate of the question linked by Paul T, because I am asking if it is possible to determine if a type is of a certain broader incomplete type/kind at compile time, not if a symbol has been registered at compile time. This seems like a fundamental misunderstanding of the question.
我正在用C语言编写一个处理伪通用函数的库,该函数通过宏包装器将类型作为自变量
I am writing a library in C that deals with pseudo-generic functions which take a type as an argument through a macro wrapper.
为了节省细节(因为它们相当复杂),我认为有两个可能的功能可能会有所帮助:
To spare the details (because they are rather complicated) there are two possible features that could help, I think:
-
能够在编译时检测类型是否是指针。 (不,使用_Generic来测试是否从减法中获取ptrdiff_t技巧不起作用,因为结构是可能的,并且您不能减去结构。)
Being able to detect if a type is a pointer at compile time. (No, the "use _Generic to test if you get ptrdiff_t from subtraction" trick won't work, because structures are a possibility, and you can't subtract structures.)
能够在编译时检测类型是否为结构。 (如果可能的话,如果检测到该类型不是结构,则可以使用前面提到的_Generic技巧。)
Being able to detect if a type is a struct at compile time. (If this was possible, then the aforementioned _Generic trick could be used if the type was detected as not being a struct.)
我已经尝试过在Godbolt上可以想到的所有方法(甚至试图将类型与不完整的匿名结构进行比较,并使用__builtin_types_compatible_p进行玩具),但找不到任何解决方案。
I've tried everything I could think of on Godbolt (even trying to compare types to incomplete anonymous structs and toying with __builtin_types_compatible_p) and wasn't able to find any solutions.
如果有人有任何解决方案,我希望看到它们,否则我可能最终不得不将设计复杂化一点,所以如果不可能的话,不要拖延世界的末日,但是如果有的话,那将是理想的选择可以做到。
If anyone has any solutions I'd love to see them, otherwise I may just end up having to complicate the design a bit-- so not the end of the world if it's impossible, but it would be ideal if it can be done.
要基本了解这些宏之一的外观或它们的预期输出:
To give a basic idea of what one of these macros might look like or their expected output:
int *a;
assert(!IS_STRUCT(a));
assert(IS_POINTER(a));
struct {} b;
assert(IS_STRUCT(b));
assert(!IS_POINTER(b));
不应抛出任何错误。
推荐答案
完整答案(如果使用了EDG前端):
如果您的IDE /编译器使用的是EDG C ++前端(其中很多),并且您使用的是 C ,而不是C ++(您的标签建议),并且您说您使用的是typeof,那么您可以按以下方式检测结构(参见最新手册,第75页):
Complete Answer (if EDG Front End used):
If your IDE / compiler is using the EDG C++ Front End (which a lot are), and you are using C, not C++ (which your tag suggests), and you say you ARE using typeof, then you can detect a structs as follows (see the latest manual, page 75):
/* Test if EDG Front End is used*/
#if defined(__EDG__) && defined(__EDG_VERSION__)
#define IS_STRUCT(expression_or_type_name) __is_class(typeof (expression_or_type_name)))
#endif
因为在 C 中 __ is_class()仅对 struct ( http://www.cplusplus.com/reference/type_traits/is_class/ )。
此外,可以类似地检测指针如下:
Further, pointers can similarly be detected as follows:
/* Test if EDG Front End is used*/
#if defined(__EDG__) && defined(__EDG_VERSION__)
#define IS_POINTER(expression_or_type_name) (__is_convertible_to(typeof (expression_or_type_name), void*) || __is_convertible_to(typeof (expression_or_type_name), void const*) || __is_convertible_to(typeof (expression_or_type_name), void volatile*) || __is_convertible_to(typeof (expression_or_type_name), void const volatile*))
#endif
( http://www.cplusplus.com/reference/type_traits/is_convertible/)
这篇关于在C编译时检查类型是结构还是指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!