Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap a function with a varying number of default arguments to have only one argument?

I have a template function, let's call it the "client":

template<typename T>
void client(T (*func)(const std::string&), const std::string& s) {}

Then there are a number of "adaptee" functions that all have an identical type of the first, non-default argument, but the following arguments vary in number and have default values:

void adaptee_one(const std::string&, int i = 1, char* c = nullptr) {}
void adaptee_two(const std::string&, float* f = nullptr) {}

The above functions are a given. Now what I want to do is to pass them to the above client<>() function as the first parameter, and I only care about passing the first argument, const std::string&. So I do the following:

void bindAdapteeOne(const std::string& s) {
    return adaptee_one(s);
}

void bindAdapteeTwo(const std::string& s) {
    return adaptee_two(s);
}

And then pass bindAdapteeX() to client<>().

What I'd like to do is to automate the wrapping or have one (templated) wrapper instead of one per adaptee. I feel this might be the case for variadics, but have little idea about how to apply them exactly.

C++11 is fine, C++14 is fine if absolutely necessary.

like image 436
iksemyonov Avatar asked Jul 06 '17 17:07

iksemyonov


People also ask

Can we overload function with default arguments?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

Can we have default arguments in functions if so how?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

How do you assign default value to a function variables give example?

Example 1: Set Default Parameter Value For a Function In the above example, the default value of x is 3 and the default value of y is 5. sum(5, 15) - When both arguments are passed, x takes 5 and y takes 15. sum(7) - When 7 is passed to the sum() function, x takes 7 and y takes the default value 5.

What is default parameter can we use default parameter for first parameter in function?

The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.


1 Answers

C++11 is fine, C++14 is fine if absolutely necessary.

C++11 solution here.

What I'd like to do is to automate the wrapping or have one (templated) wrapper instead of one per adaptee.

I wouldn't do that. You can simply use non-capturing lambdas and let them decay to function pointers:

client (+[](const std::string& s) { return adaptee_one(s); }, "foo");

I don't think that wrapping them in template stuff or whatever would give you a solution that is more readable or easy to use.


As a minimal, working example:

#include<string>

template<typename T>
void client(T (*func)(const std::string&), const std::string& s) {}

void adaptee_one(const std::string&, int i = 1, char* c = nullptr) {}
void adaptee_two(const std::string&, float* f = nullptr) {}

int main() {
    client (+[](const std::string& s) { return adaptee_one(s); }, "foo");
}
like image 181
skypjack Avatar answered Oct 11 '22 09:10

skypjack