c++ - 您如何使用 "function type"?

标签 c++

在 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/

相关文章:

c++ - 创建一个引用另一个 vector 的某些元素的 vector

c++ - 在成员初始化程序中创建临时对象时销毁它的点

c++ - 在函数顶部声明的变量和后面声明的变量之间的区别

c# - 不同机器执行相同API的时间差

c++ - 在 C++ 中存储对对象的引用需要多少内存?

c++ - GLEW 链接错误

c++:如果条件不满足,函数调用会出现编译时错误?

c++ - Qt QException 子类抛出,但 QUnhandledException 被捕获

c++ - 有没有办法使用STL优雅地填充矩阵( vector 的 vector )?

c++ - 分割数据进行多线程处理