MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/cpp/comments/980zc0/implementing_default_parameters_that_depend_on/e4diu2o/?context=3
r/cpp • u/vormestrand • Aug 17 '18
9 comments sorted by
View all comments
37
or just
void f(double x, double y, double z) { //... } void f(double x, double y) { f(x, y, x+y); }
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".
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.
1
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.
6
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.
4
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.
const
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".