0

Question

Let's say I have a function:

int foo(int a, int b, int c = 5, int d = 10){
    return a + b + c + d;
}

How can I call it providing a, b and d but not c?

Concrete example

I want to use cv::erode documented here


void cv::erode  (   InputArray  src,
OutputArray     dst,
InputArray  kernel,
Point   anchor = Point(-1,-1),
int     iterations = 1,
int     borderType = BORDER_CONSTANT,
const Scalar &  borderValue = morphologyDefaultBorderValue() 
)   

and provide (except of required arguments) only iterations, but use default value for anchor. Apparently, pythonic approach does not work cv::erode(img, out, kernel, iterations=radius); :(

hans
  • 1,043
  • 12
  • 33
  • 1
    Although I'll save you the click. The answer is, unfortunately: you can't. – Silvio Mayolo May 10 '21 at 17:19
  • Call it as if there is no argument d, like foo(a,b,c); – debanshu das May 10 '21 at 17:19
  • Create three overloads of your function `foo`: One taking only `a` and `b` as arguments, it calls `foo(a, b, 5, 10)`; One taking `a`, `b` and `d` as arguments, it calls `foo(a, b, 5, d)`; And the last taking all four arguments without default values. – Some programmer dude May 10 '21 at 17:22
  • @Someprogrammerdude Overloads won't work here because the two shorter overloads would both have signature `int foo(int, int, int)` and would be indistinguishable. – Silvio Mayolo May 10 '21 at 17:23
  • Hmm, that seems a bizarre limitation of the language... Thanks for help. – hans May 10 '21 at 17:23
  • default arguments are rather limited. Consider that its a whole different story in languages that have named parameters, which C++ doesnt. As a caller, how would you tell what parameters are explicitly given and for which the defaults should be used in eg: `foo(1,2,3)`. There needs to be a rule and that is: parameters may only be ommited starting from right – 463035818_is_not_an_ai May 10 '21 at 17:27
  • @SilvioMayolo Three overloads: One taking *two* arguments; One taking *three* arguments; And the last taking *four* arguments. And none of them with default argument values. – Some programmer dude May 10 '21 at 17:34

0 Answers0