[英]Overloading a function with void
Trying to overloading a function in C programming language with _Generic
. 尝试使用_Generic
重载C编程语言中的函数。 Have found examples: 找到了示例:
This the example: 这个例子:
#include <stdio.h>
#include <math.h>
void display_float(float dispid) ;
void display_int(int dispid) ;
void display_void(void) ;
#define display(x) _Generic((x), \
float: display_float, \
int: display_int, \
default: display_float \
)(x)
void display_void(void){
printf("display id: 0\n") ;
}
void display_float(float dispid){
printf("display id: %f\n", dispid) ;
}
void display_int(int dispid){
printf("display id: %d\n", dispid) ;
}
void main(void){
display(5) ;
display(6.5) ;
}
Now I also want to overload the function with display()
. 现在我也想用display()
重载该函数。 Meaning the function would intake a void
, call the function display_void()
, and display 0. Can't seem to do this. 意味着该函数将接收一个void
,调用函数display_void()
并显示0。似乎无法做到这一点。 Any help would be appreciated. 任何帮助,将不胜感激。
EDIT 1: 编辑1:
Also, see the examples here . 另外,请参阅此处的示例。 One of the examples (I think) is passing a pointer to a void. 我认为,其中一个示例是传递指向void的指针。 Can this be implemented? 可以实施吗?
Sure. 当然。 Overload Macro on Number of Arguments . 重载参数宏 。
void display_float(float dispid) ;
void display_int(int dispid) ;
void display_void(void) ;
#define _display_1(x) _Generic((x), \
float: display_float, \
int: display_int, \
default: display_float \
)(x)
#define _display_0() display_void()
#define _display_N(_0,_1,N,...) _display_ ## N
#define display(...) _display_N(_0,##__VA_ARGS__,1,0)(__VA_ARGS__)
int main() {
display();
display(1);
display(1.1);
}
Tested on godbolt . 测试在godbolt 。
Note that , ##__VA_ARGS__
is a gcc extension not described in the C standard. 请注意, ##__VA_ARGS__
是gcc扩展,未在C标准中进行描述。
Trying to overloading a function in C programming language 尝试重载C编程语言中的函数
C11 does not have any function overloading and _Generic
is for genericity , not for overloading, check that by reading n1570 . C11没有任何函数重载 , _Generic
是泛型的 , 不是重载的,请通过阅读n1570进行检查。 Consider switching to C++ (or even better, Rust ) which provides function overloading. 考虑切换到提供函数重载的C ++(或者更好的是Rust )。 Notice that it is illegal to have a void
(not void*
, just void
) argument to a C function, so there is no any "top" super-type in C, which would be a super type of other types (and this because of calling conventions and ABI considerations making C a low-level programming language, sometimes even called a " portable assembler"). 请注意,对C函数使用void
(不是void*
,只是void
)参数是非法的,因此C中没有任何“ top”超类型,它可能是其他类型的超类型(这是因为调用约定和ABI注意事项使C成为低级编程语言,有时甚至称为“ 可移植汇编程序”)。 Read more about abstract interpretation and type systems please. 请阅读有关抽象解释和类型系统的更多信息。
If _Generic
was for overloading, the members of the C11 standard committee - all programming language experts and good enough English writers - would have named it like _Overload
, but they wisely did not. 如果_Generic
用于重载,则C11标准委员会的成员-所有编程语言专家和足够好的英语作者-都将其命名为_Overload
,但他们明智地没有这么做。
Food for thought (assuming both stderr
and stdin
works like you want them to work) : 值得深思 (假设stderr
和stdin
像您希望它们工作一样工作) :
what about some hypothetical display(stderr)
or display(*stdin)
or display("abc")
? 假设的display(stderr)
或display(*stdin)
或display("abc")
怎么办?
(a wrong suggestion below, followed by a better advice) (以下错误建议,然后是更好的建议)
( this does not work! ) ( 这不起作用! )
Your display_void
should take some argument, eg 您的display_void
应该带有一些参数,例如
void display_void(void*p) { printf("p@%p\n", p);
Then you might have: 然后,您可能会:
#define display_anything(P) display_void((void*)(&(P)))
with 与
#define display(x) _Generic((x), \
float: display_float, \
int: display_int, \
default: display_anything \
)(x)
Since that does not work (and it does not), design your code in some other way . 由于这行不通(而且行不通),因此可以采用其他方式设计代码 。 For example, consider having some tagged union , like here . 例如,考虑使用一些标记的联合 ,例如here 。 Look into GVariant (from Glib) implementation for inspiration. 查看GVariant (来自Glib)实现的灵感。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.