有没有一种方法可以使用来自typedef的参数名称 [英] Is There a way to use the Parameter Names from a typedef
问题描述
因此,给定一个typedef
,它使用以下参数名称定义函数指针:
So given a typedef
that defines a function pointer with parameter names like this:
typedef void(*FOO)(const int arg);
有没有一种方法可以仅使用此函数指针来定义函数的签名?显然这是行不通的,但是我想以某种方式使用typedef
来指定具有相应类型的函数签名:
Is there a way that I can just use this function pointer to define the signature of my function? Obviously this won't work, but I'd like to somehow use the typedef
to specify a function signature with a corresponding type:
FOO foo {
cout << arg << endl;
}
同样,我知道这行不通,并且语法错误.它只会给出错误:
Again, I know this doesn't work, and is bad syntax. It will just give the error:
错误:在此范围内未声明
arg
推荐答案
您尝试执行的操作无效. FOO
是void(*)(const int)
的别名.所以
What you are trying to do will not work. FOO
is an alias for void(*)(const int)
. so
FOO foo {
cout << arg << endl;
}
成为
void(*)(const int) foo {
cout << arg << endl;
}
那是行不通的.但是,您可以做的是定义一个带有名称的宏,并使用该宏标记功能签名.看起来像
and that just doesn't work. What you can do though is define a macro that takes a name and use that to stamp out a function signature. That would look like
#define MAKE_FUNCTION(NAME) void NAME(const int arg)
MAKE_FUNCTION(foo){ std::cout << arg * 5 << "\n"; }
MAKE_FUNCTION(bar){ std::cout << arg * 10 << "\n"; }
int main()
{
foo(1);
bar(2);
}
这篇关于有没有一种方法可以使用来自typedef的参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!