在 C++ 中,您可以创建一个“函数类型”,例如:
void main() {
int a();
}
a 的类型是“int()”,但是可以使用吗?我什至不能将“a”作为模板参数传递(但我可以将“int ()”作为一个参数传递)
最佳答案
您没有声明“函数类型”。您正在声明一个函数。这与您通常在文件范围内执行的操作相同,但在本例中您在本地范围内执行
int main() {
int a(); /* declare function `a` */
...
int i = a(); /* call function `a` */
}
int a() { /* define function `a` */
/* whatever */
}
是的,您可以将其作为模板参数传递。当然,它必须是非类型参数
template <int A()> void foo() {
A(); /* call the function specified by the template argument */
}
int main() {
int a(); /* declare function `a` */
foo<a>(); /* pass it as a template argument */
}
关于c++ - 您如何使用 "function type"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5439811/