你怎么用"功能类型"?

use*_*445 2 c++

在C++中,您可以创建一个"函数类型",例如:

void main() {
 int a();
}
Run Code Online (Sandbox Code Playgroud)

并且a具有"int()"类型,但是可以使用它吗?我甚至无法传递'a'作为模板参数(但我可以将"int()"作为一个传递)

AnT*_*AnT 5

您没有声明"功能类型".你正在宣布一个功能.这与您通常在文件范围中执行的操作相同,但在这种情况下,您可以在本地范围内执行此操作

int main() {
   int a(); /* declare function `a` */
   ...
   int i = a(); /* call function `a` */
}

int a() { /* define function `a` */
  /* whatever */
}
Run Code Online (Sandbox Code Playgroud)

是的,您可以将其作为模板参数传递.当然,它必须是非类型的论点

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 */
}
Run Code Online (Sandbox Code Playgroud)