r/cpp • u/anonymous23874 • Apr 19 '20
Default function arguments are the devil
https://quuxplusone.github.io/blog/2020/04/18/default-function-arguments-are-the-devil/
88
Upvotes
r/cpp • u/anonymous23874 • Apr 19 '20
57
u/EsotericFox Apr 19 '20
I think that when you're creating an API you should provide as much flexibility as possible, while retaining ease-of-use for the typical consumer.
This is where default arguments shine. MOST people won't require or be interested in the additional parameters which functions might provide to enable further functionality. As a developer, I could wrap that up in parameter structs or mandate the arguments during invocation, but that becomes cumbersome in and of itself.
I simply don't agree.
38
u/jbandela Apr 19 '20
C++20 designated initializers with NSDMI (combined with default arguments) allow for an elegant solution to print_squares
https://gcc.godbolt.org/z/-r6U3P
You have a single implementation. There is no question about what overload is getting called. There is a single point of reference for all the defaults (the definition of print_square_args).
96
u/fransinvodka Apr 19 '20
I think default arguments are actually useful, but they lack one important, complement feature: Named Arguments.
Sometimes, a set of overloads works just fine. Some others you can just let the user name the ones they really need. I just like the way Python solved this.
I know there's problems implementing this feature into the language due to the declaration-definition nature of C++ functions, but I really think the ultimate fix to default arguments are named arguments.
45
u/whattapancake Apr 19 '20
Just sounds like a case of throwing the baby out with the bath water to me. Of course there are downsides to certain language features - in fact, C++ is chock full of footguns much worse than those the author points out about default arguments. That doesn't mean we should avoid these features like the plague, but rather use them modestly and only when appropriate.
21
u/MrPotatoFingers Apr 19 '20
My greatest peeve with it is that type traits are simply unaware of default arguments. std::is_invocable will happily report false when omitting default arguments.
8
u/Dragdu Apr 19 '20
You can make source location work with variadics by liberal appli ation of CTAD, or, of course, macros.
I also find the boolean tarpit argument extremely dubious: the best way to deal with it is to leave the boolean tarpit, not avoid default args.
8
u/Morwenn Apr 19 '20
One thing not addressed here is that you can generally add overloads to a function without breaking the ABI while you can't afford to add default arguments to a function if ABI is something you care about.
8
u/sphere991 Apr 19 '20
I guess Arthur must really hate Ranges?
There, we have algorithms like (this pattern is prolific throughout algorithms):
Which this post claims is "the devil", and should be rewritten like:
Personally, I strongly prefer the former (note that the predicate constraint needs to be spelled differently in each overload). Your mileage may vary.
7
u/YouNeedDoughnuts Apr 19 '20
It's syntatic sugar to make definitions more terse in a way that improves readability, IMO. Not technically syntatic sugar since it has some differences with function pointers and inheritance as you pointed out... syntatic Splenda I guess. I'll still eat it, but I did enjoy the article and learn from it.
7
u/kritzikratzi Apr 19 '20 edited Apr 19 '20
i disagree a bit. when used sparingly, default arguments are still handy here and there.
for me the main conclusion missing from the article is that c-style struct initializers would be a good enough replacement for named parameters. e.g.
doTask({.task=task1,.captureStdout=true});edit: just found out we got half-baked designated initializers in c++20. could be better, but i'll happily take it :)
24
u/jstock23 Apr 19 '20
It is quite contrived.
12
u/sephirothbahamut Apr 19 '20 edited Apr 19 '20
I fucking hate any article that declares something to be "evil" since the famous Dijkstra one (which title he didn't even agree with afaik).
Anyways the problem in the example is more a char-int default conversion problem really. Nothing wrong with default arguments.
print_square('*')should simply be an error as char should not be implicitely converted to int.4
u/georgist Apr 19 '20
I think automatic type conversion for non-built-in types is evil.
If I pass a string to a function that takes a Widget and Widget has a string constructor, why do I ever want that automagically converted?! I know I can avoid this with explicit, but I really don't like this feature.
1
u/DVMirchev C++ User Group Sofia Apr 22 '20
If you want to add to the confusion there is also default function arguments initialized by a function call.
#include <iostream>void moo(int a = [](){ static int b = 1; return b++; }() ){std::cout << a;}int main(){moo();moo();}Anyone care to guess what's happening here without googling? :D
2
u/Omnifarious0 Apr 19 '20 edited Apr 19 '20
Don't do this:
void doTask(Task task, std::seconds timeout, bool detach = false, bool captureStdout = false, bool captureStderr = false);Do this:
1
u/khleedril Apr 19 '20 edited Apr 19 '20
I thought that was an excellent read and I agreed with every word of it. I also think that overloaded functions should more often have different names, because fundamentally they usually provide a different service to the rest of the code base. Default arguments are often thrown in by end users who can't be bothered to express their intentions properly.
I would say the same about overloaded constructors: distinctly named functions returning newly constructed objects are the order of the day (we are talking C++20 code here!).
0
u/NilacTheGrim Apr 19 '20
What? I beg to disagree, sir. Default function arguments are the bees knees Bees knees, I say!
-12
u/bumblebritches57 Ocassionally Clang Apr 19 '20
lol, damn y'all mad
-29
u/bumblebritches57 Ocassionally Clang Apr 19 '20
ABSOLUTE FACT.
It's super confusing seeing a function being called using 1 argument when it actually takes 3.
-6
u/cat_vs_spider Apr 19 '20
the Cpp core Guidelines are wrong on many things, and this is one of them. It’s a document with noble goals, but the road to hell is paved with noble goals.
In my experience, functions grow default parameters like a fungus. Somebody adds one with a “sane” default and doesn’t tell anyone. Since nobody knows about it, they don’t ever set it and a bunch of code is now suddenly buggy. It’s always laziness because refactoring is hard and they just want to land this feature.
Default arguments are evil.