default of function alias arg.

Is there a way to set the default of function alias arg. so having option to be one or two argument of its each own purpose, without the way of overload multiple functions (as it is large enough to extra work)

let's realize this illustration:

1
2
3
4
5
6
7
8
int fun(char* str, int& len=0) {

  char* p=str;
  //if exist 2nd arg. do operation below, otherwise none
   len += 9;
  return atoi(p) + len;

}
Last edited on
note that in your code sample, second argument always exists, wether specified or not by the user.

if you want to omit second argument from function call, but still be able to provide it you have 2 options.

1. Use C style varaible args function.
2. Make function as variadic template

Pesudo sample variadic template:
1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename... Args>
int fun(char* str, Args params)
{
      if (sizeof... (Args))
      {
            // additional arguments are available
            // put conditional code here
      }         
      else
      {
            // there is no additional arguments
      }
}
Last edited on
Topic archived. No new replies allowed.