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

标签 c++ templates c++17 typetraits

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

相关文章:

c++ - 用一个vector < pair < string, int >>来表示一个哈希表

c++ - c++ 中是否有一个方法/函数,后来的常量参数基于第一个参数?

c++ - (boost::)outcome 的错误处理:在具有组合和继承的构造函数中使用

c++ - 返回 `std::string_view` 不包括第一个字符

c++ - 不确定 Dekkers 算法做错了什么

c++ - 在 linux 下链接库时 Cython 构建问题

c++ - 如何使用模板创建多维 std::array?

c++ - 成员函数中静态变量的替代方法

c++ - 使用使用成员类型别名的构造函数推导出类模板参数

c++ - 如何保证进程调用malloc()时立即分配物理内存?