我有一个模板函数,有几个std :: function类型参数,我想默认其中一个参数,这样调用它的其他程序就不需要指定那些输入参数了。例如:
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) {...}
我想将param_3
设置为默认值1.0
,所以如果我想调用此模板函数,我只需要这样调用它:
dataStruct<int, int> solution = foo(input_fun1, input_fun2);
如果param_3
的值只是默认1.0
,我就不需要指定std::function<double(T1, T1)> param_3 = setToOne()
。
我知道我可以用更少的参数重载它,但是有什么方法可以跳过这个重载?喜欢直接在这个模板函数中设置默认值吗?
Range("AS3:AS" & Range("B" & Rows.Count).End(xlUp).Row).formula = "=Right(""000"" & Row(AS3) - 2,4)"
Range("AS:AS").copy
Range("AS:AS").PasteSpecial xlPasteValues
这样的事情?
答案 0 :(得分: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; })
{
//...
}
答案 1 :(得分:2)
I want to set
param_3
to have a default value of1.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) { ... }