无操作函数指针 [英] no-op function pointer
问题描述
啊......在我去特定平台寻找专门的
答案之前,我想我会看看是否有便携式C答案/>
问题:
我想要一个函数指针,当被调用时,可以是真正的无操作。
考虑:
typedef int(* polymorphic_func)(int param);
struct various_funcs {
polymorphic_func func_a ;
polymorphic_func func_b;
};
int really_does_something(int param){return ++ param; }
int really_a_noop(int param){(void)param;返回0; }
struct various_funcs does_stuff = {really_does_something,
really_does_something};
struct various_funcs noop_funcs = {reall_a_noop,really_a_noop};
int main(int argc,char ** argv){
int x = 10;
int y = does_stuff.func_a (x);
int z = noop_funcs.func_b(x);
返回z;
}
----------------------------------
很多打字非常明显;但引用
noop_funcs.func_b(x)实际上是否进行函数调用?或者
编译器是否只需将0分配给z?有没有C方式来确保noop?或者
它将是关于哪个编译器/哪个平台?
或者编译器似乎不太可能将其优化为
noop?
当然,我真的在考虑一个变量
包含一个只能是的函数指针的场景在
运行时被称为noop,这实际上使我的
思想中的语言问题比编译器问题更多。如果NULL函数指针是noop,
这将非常方便。但是当然,NULL函数指针
不是noop,它是一个空指针,并且取消引用它是一个段错误。
所以有语言支持什么都不做的函数指针?
想法?重定向?
Ahoy... before I go off scouring particular platforms for specialized
answers, I thought I would see if there is a portable C answer to this
question:
I want a function pointer that, when called, can be a genuine no-op.
Consider:
typedef int(*polymorphic_func)(int param);
struct various_funcs {
polymorphic_func func_a;
polymorphic_func func_b;
};
int really_does_something(int param) { return ++param; }
int really_a_noop(int param) { (void)param; return 0; }
struct various_funcs does_stuff = { really_does_something,
really_does_something };
struct various_funcs noop_funcs = { reall_a_noop, really_a_noop };
int main (int argc, char** argv) {
int x = 10;
int y = does_stuff.func_a(x);
int z = noop_funcs.func_b(x);
return z;
}
----------------------------------
A lot of typing for the very obvious; but does the reference to
noop_funcs.func_b(x) actually make a function call? Or would the
compiler simply assign 0 to z? Is there any C way to ensure a noop? Or
is it going to be all about which compiler / which platform?
Or does it seem unlikely that a compiler could optimize that down to a
noop?
Naturally, I''m really thinking about scenarios in which a variable
contains a function pointer that can only be known as a noop at
runtime, which actually makes this MORE of a language problem in my
mind than a compiler problem. If the NULL function pointer were a noop,
this would be very convenient. But of course, the NULL function pointer
is not a noop, it''s a null pointer, and dereferencing it is a segfault.
So is there language support for a function pointer that does nothing?
Ideas? Redirection?
推荐答案
我假设你的函数指针是空的空操作。在
之前,你调用函数验证是否为NULL。如果它是NULL
那么make noop(;)或者调用函数。
I assume your function pointer will be null when it is noop. Before
you call the function verify whether thats NULL or not. If it is NULL
then make noop( ; ) or else call the function.
检查NULL并且如果不是NULL则调用是,呃,比我想要的更少的no / b $ b b b b b b b b b g g g g g g g g g b b b b b b b b b b b这是我所希望的最好的。
Checking for NULL and calling if not NULL is, erm, less of a noop than
I was hoping for, but conversations with some guys over at the gcc
newsgroup suggest that''s about the best I can hope for.
2005年12月13日22:31:07 -0800,bluejack < BL ****** @ gmail.com>在comp.lang.c中写了
:
On 13 Dec 2005 22:31:07 -0800, "bluejack" <bl******@gmail.com> wrote
in comp.lang.c:
检查NULL并调用如果不是NULL,那么,呃,不是一个noop而不是<我一直希望,但是在gcc
新闻组中与一些人的对话表明,这是我所希望的最好的。
Checking for NULL and calling if not NULL is, erm, less of a noop than
I was hoping for, but conversations with some guys over at the gcc
newsgroup suggest that''s about the best I can hope for.
将你的无操作函数放在不同的源文件中并单独编译它。
在将其地址分配给指针的函数中,仅
一个外部定义。
这应该确保函数被调用。
-
Jack Klein
主页: http:// JK- Technology.Com
常见问题解答
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp .lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++
http:// www .contrib.andrew.cmu.edu / ~a ... FAQ-acllc.html
Put your no-op function in a different source file and compile it
separately.
In the function where you assign its address to a pointer, only have
an external definition.
That should make sure the function is called.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
这篇关于无操作函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!