Search code examples
c++c++11default-parametersfunction-parameter

Is there a simple way to call a function with default arguments?


Here is a function declaration with default arguments:

void func(int a = 1,int b = 1,...,int x = 1)

How can avoid calling func(1,1,...,2) when I only want to set the x parameter and for the rest with the previous default params?

For example, just like func(paramx = 2, others = default)


Solution

  • You can't do this as part of the natural language. C++ only allows you to default any remaining arguments, and it doesn't support named arguments at the calling site (cf. Pascal and VBA).

    An alternative is to provide a suite of overloaded functions.

    Else you could engineer something yourself using variadic templates.