Lightning talk #3 – Default function arguments (DFA)

Lightning talk #3 – Default function arguments (DFA)

Default function arguments are expressions that are evaluated when there are fewer provided args to a function call than the number of parameters specified in the function definition.

Simple example

You can have a default function argument in:

  • member functions
  • parameters in lambda
  • function templates

Advice: Do not use default function arguments, except in the simplest cases.

Mixing DFAs and overloading can cause ambiguous lookup

2. Scope, lookup, and multiple declarations

Names in a DFA are bound at declaration, but evaluated at use 

1. DFAs can be provided across multiple declarations of the same function

The default arguments are additive! The set of default arguments is a UNION of the default arguments provided in the declarations.

2. The first declaration in a scope hides any previously provided DFAs.

3. Using declarations (pulling names into the scope) can cause surprises 

If someone calls using namespace in other file, you may get unexpected results.

4. DFAs on overridden virtual member functions

Virtual methods and DFA do not play well with each other

The problem is that default args are determined from the static type while the implementation is picked due to dynamic dispatch.

You may also like...

Leave a Reply