#include <thread>
#include <functional>
using namespace std;
void f(int n = 7)
{}
void g(function<void()> fn)
{
fn(); // same as f(7)
}
template<typename Callable>
auto GetDefaultArg(Callable fn, size_t arg_ordinal)
{
// What to put here?
}
int main()
{
auto fn = bind(f, GetDefaultArg(f, 1));
g(fn);
}
如上面的代码所示,我想实现一个模板函数 GetDefaultArg
来检测函数的默认参数。
在当前的 C++ 中有可能吗?
最佳答案
不,你不能在编译时检测默认参数。
您已经在函数定义中声明了默认参数。但是默认参数并没有链接到函数本身,而是链接到函数调用范围内已知的函数声明。
另外说明:您可以为同一功能设置不同的默认参数集。
关于c++ - 是否可以在编译时检测函数的默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42920457/