Is it possible to define default function parameters at compile time?

#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 shown in the above code, I want to implement a template function GetDefaultArg

to define default parameters for the function.

Is this possible in current C ++?

+3


source to share


2 answers


No, you cannot define a default option at compile time.

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



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

+5


source


To add to Chrisoph 's excellent comment , I'll give you an example of what he means.

We can reuse the function later with additional defaults!

What does it mean?

Let's start by declaring a function:

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

      

I have some function foo

with no default arguments. I am writing a function that calls foo

:

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

      

It must provide an argument for each parameter in the foo

.

However, now I can redeclare foo

(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 for this time, I can leave the last argument empty:



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

      

I can keep doing this, updating with a new default every 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 update, the foo

declaration foo

consists of concatenating the default values ​​(note: I cannot re-set the value).

Then finally I can give a 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 try it 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

+2


source







All Articles