With the classic 5 argument function, there is no way to give it only 3 or 4. You can only write 3 or 4 with the default arguments, but in the end you will get a function call with 5 arguments.
There are also problems with your system if there are several parameters of the same type. For example, if you have foo(int a=4,int b=5) and a call to foo(10) , how do you know you want to call foo(10,5) or foo(4,10) ?
With C ++ 11 tuples and idiom named parameters , you can trick it a bit.
#include <iostream> #include <functional> #include <tuple> #include <string> struct f_ { private: typedef std::tuple<int,int,double> Args; //default arguments static constexpr const Args defaults = std::make_tuple(10,52,0.5); Args args; public : f_():args(defaults) {} template <int n,class T> f_& setArg(T&& t) { std::get<n>(args) = t; return *this; } void operator()() { return (*this)(std::move(args)); } void operator()(Args&& a) { int n1=std::get<0>(a); int n2=std::get<1>(a); double n3=std::get<2>(a); std::cout<<n1<<" "<<n2<<" "<<n3<<std::endl; } }; #define set(n,v) setArg<n>((v)) int main() { //f_().set<1>(42).set<3>("foo") (); f_().setArg<1>(42)(); //without Macro f_().set(0,666).set(1,42)(); //with Macro f_()(); //without any parameters f_()(std::forward_as_tuple(-21,-100,3.14)); //direct call }
An alternative method is to use std :: bind as described there
Davidbrcz
source share