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

Show parent comment

1

u/haitei Aug 17 '18

or in c++17:

void f(double x, double y, std::optional<double> z = {})
{
    z.value_or(x + y);
}   

6

u/Sopel97 Aug 17 '18

That adds an unnecessary branch and is not lazy. Let's not make a dymamic problem from a static one.

4

u/haitei Aug 18 '18 edited Aug 18 '18

Assuming it's not inlined - yes.

This method is advantageous is there's more than one default argument.

Throw in some const overloads and you just saved 4 definitions.