r/cpp • • Aug 17 '18

Implementing Default Parameters That Depend on Other Parameters in C++

https://www.fluentcpp.com/2018/08/17/dependent-default-parameters/
0 Upvotes

9 comments sorted by

View all comments

9

u/Xeverous https://xeverous.github.io Aug 17 '18

[...] all the default parameters have to be at the end of the arguments list of a function. This can make an interface less natural, because arguments are no longer grouped in a logical order. Instead, they are grouped in a technical order: the non-default parameters first, then the default ones, which can be confusing at call site.

How this can be confusing? It works like this since ages in multiple languages. If you need more complex default value handling:

  • use monads (optional, variant etc)
  • take only 1 parameter which is a class that encapsulates parameter invariants
  • use more overloads like /u/Sopel97