基本上,我想要一个能够使用默认参数调用的函数指针。请看以下内容:
#include <iostream>
using namespace std;
int function(int arg1 = 23)
{
printf("function called, arg1: %d\n", arg1);
return arg1 * 3;
}
template<typename Fn> int func(Fn f)
{
int v = 3;
f(); //error here 'error: too few arguments to function'
f(23); //this compiles just fine
return v;
}
int main() {
func(&function);
printf("test\n");
return 0;
}
是否有任何方法(诡计或其他方式)能够在不显式指定参数的情况下使用函数指针(或模板参数)的默认参数调用函数?
最佳答案
是的,有一个好方法。函数对象。我强烈建议您查看此链接。
http://www.stanford.edu/class/cs106l/course-reader/Ch13_Functors.pdf
关于C++(11) 函数的默认参数作为模板参数或函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983132/