是否可以在编译时检测函数的默认参数?

xml*_*lmx 3 c++ templates type-traits c++17

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

如上面的代码所示,我想实现一个模板函数GetDefaultArg来检测函数的默认paratemters.

它在当前的C++中是否可行?

Chr*_*phe 5

不,您无法在编译时检测到默认参数.

您已在函数定义中声明了默认参数.但是默认参数没有链接到函数本身,而是在函数调用范围内已知的函数声明.

否则说明:您可以为同一功能设置不同的默认参数集.