Is it possible to detect the default parameters of a function at compile-time?

我与影子孤独终老i 提交于 2019-12-10 21:44:11

问题


#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);  
}

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

It it possible in current C++?


回答1:


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.




回答2:


To add to Chrisophe's excellent answer, I'll provide an example of what he means.

We can redeclare a function later with additional defaults!

What does this mean?

Let's start with a function declaration:

// initial declaration; nothing defaulted
void foo(int a, char b, bool c, double d);

I have some function foo with no default arguments. I write a function that calls foo:

void CallFoo1()
{
    // no default params
    foo(1, 'b', true, 2.0);
}

It must provide an argument for every parameter in foo.

However, now I can redeclare foo (so long as I haven't defined it yet), and give the last parameter a default value:

// add default for last argument
void foo(int a, char b, bool c, double d= 2.0);

Then I write another function that calls foo. Except this time I can leave the last argument empty:

void CallFoo2()
{
    foo(1, 'b', true);
}

I can continue to do this, redeclaring with a new defaulted value each time:

// add default for third argument
void foo(int a, char b, bool c=true, double d);

void CallFoo3()
{
    foo(1, 'b');
}

// add default for second argument
void foo(int a, char b='b', bool c, double d);

void CallFoo4()
{
    foo(1);
}

// add default for first argument
void foo(int a=1, char b, bool c, double d);

void CallFoo5()
{
    // everything is defaulted!
    foo();
}

After the last redeclaration of foo, the declaration of foo consists of the union of defaulted values (note: I can't re-default a value).

Then finally I can provide the definition for foo:

void foo(int a, char b, bool c, double d)
{
    std::cout << "a = " << a << " b = " << b << " c = " << std::boolalpha << c << " d = " << d << std::endl;
}

And let's try it out with a test:

int main()
{
    CallFoo1();
    CallFoo2();
    CallFoo3();
    CallFoo4();
    CallFoo5();
}

Output:

a = 1 b = b c = true d = 2
a = 1 b = b c = true d = 2
a = 1 b = b c = true d = 2
a = 1 b = b c = true d = 2
a = 1 b = b c = true d = 2

Live Demo



来源:https://stackoverflow.com/questions/42920457/is-it-possible-to-detect-the-default-parameters-of-a-function-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!