0

I have a template function, with several std::function type arguments, I want to default one of these arguments, such that the other programs calling it will not need to specify that input arguments. For example:

template<class T1, class T2>
dataStruct<T1, T2> foo(std::function<T2(T1, T1)> param_1, 
                       std::function<T2(T1, T1)> param_2,
                       std::function<double(T1, T1)> param_3) {...}

I want to set param_3 to have a default value of 1.0, so if I want to call this template function, I only need call it like this:

dataStruct<int, int> solution = foo(input_fun1, input_fun2);

such that I don't need to specify param_3 if its value is just default 1.0. I know I can overload it with less parameters, but any way to skip this overload? like set default value in this template function directly? std::function<double(T1, T1)> param_3 = setToOne() something like this?

1
  • You could set the default value of param_3 to a static function that will take your parameters and always return 1.0, but depending on your implementation, it would probably end up being less readable than the answer below. Commented Jul 28, 2015 at 22:22

2 Answers 2

3

You could use a lambda as a default parameter:

template<class T1, class T2>
dataStruct<T1, T2> foo(std::function<T2(T1, T1)> param_1,
                       std::function<T2(T1, T1)> param_2,
                       std::function<double(T1, T1)> param_3 = [](T1, T1) { return 1.0; })
{
    //...
}
Sign up to request clarification or add additional context in comments.

4 Comments

Extrapolated to the OP's code: template<class T1, class T2> dataStruct<T1, T2> foo( std::function<T2(T1, T1)> param_1, std::function<T2(T1, T1)> param_2, std::function<double(T1, T1)> param_3 = [](T1, T1) { return 1.0; }) { ... }
You're right, I should have done that in the first place. Edited into the answer.
Using lambda, I still need to specify this param_3 when call foo(), right? I tried on my complier and return no match function error.
@EmersonXu: Your param_N may be a function pointer (e.g. T2 (*)(T1, T1) and not a std::function<T2(T1, T1)>, so you need to help the compiler figure out which instantiation of foo you want to call. One way to do this is to explicitly specify the template arguments T1 and T2 when calling foo: e.g. foo<int, std::string>(p1, p2). Another way is to convert your function pointers to std::function before calling foo: e.g. std::function<int, std::string> f1 = p1;, and then pass f1 et al. to foo instead of p1 et al.
1

I want to set param_3 to have a default value of 1.0.

I don't think you can do that. The value of a std::function<double(T1, T1)> cannot be 1.0. One option is to overload the function.

template <class T1, class T2>
dataStruct<T1, T2> foo(std::function<T2(T1, T1)> param_1, 
                       std::function<T2(T1, T1)> param_2,
                       double param_3 = 1.0) { ... }

1 Comment

There are plenty of ways you can achieve this, e.g. this somewhat silly idea or as the other answer points out, a lambda.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.