r/cpp • u/vormestrand • 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
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
37
u/Sopel97 Aug 17 '18 edited Aug 17 '18
or just
I know this may be the obvious way, but it's so much cleaner I wouldn't even consider writing a blog post about your "solution".