是否可以在编译时检测函数的默认参数? [英] Is it possible to detect the default parameters of a function at compile-time?

查看:90
本文介绍了是否可以在编译时检测函数的默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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 来检测函数的默认参数。

As illustrated in the code above, I want to implement a template function GetDefaultArg to detect the default paratemters of a function.

在当前的C ++中可以吗?

It it possible in current C++?

推荐答案

否,在编译时无法检测默认参数。

No you can't detect the default parameter at compile time.

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

You have declared the default parameter within the function definition. But default parameters are not linked to the function itself, but the declaration of the function that is known in the scope where the function is called.

否则,您可能已经知道相同功能的不同默认参数集。

Otherwise stated: you could have different sets of default parameters for the same function.

这篇关于是否可以在编译时检测函数的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆