3

Using ReSharper with C++17, and I enabled many of the warnings just to see what my project warns me about. I get this:

Declaring a parameter with a default argument is disallowed[fuchsia-default-arguments]

The code in question is the constructor:

class Point2D
{
public:
    explicit Point2D(double x = 0.0, double y = 0.0);
};

I'm wondering why default arguments would be considered bad/poor/worthy of a warning? Does anyone have any code examples proving this a viable warning?

Here is the documentation.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
keelerjr12
  • 1,693
  • 2
  • 19
  • 35
  • 2
    I don't know much about ReSharper but it appears you've enabled clang-tidy's fuschia-* family of checks. That project's coding conventions must frown upon the use of default arguments, that doesn't mean they're inappropriate in general. – Praetorian Jul 11 '18 at 18:14
  • @Praetorian, yah I'm just going to turn them off. I was just curious for my own understanding, why a team would find this to be bad. Thank you! – keelerjr12 Jul 11 '18 at 18:16
  • 2
    Related: https://stackoverflow.com/questions/51217635/is-there-a-way-to-avoid-this-warning-from-clang-tidy-fuchsia-default-arguments – tkausl Jul 11 '18 at 18:19
  • I’m not endorsing this, but some folks think it’s better to write a set of overloaded functions than a single one with default arguments. – Pete Becker Jul 11 '18 at 18:47
  • Recommend removing the c++17 tag as this behavior is not changed in that version of the standard. – Michael Price Jul 12 '18 at 04:25

1 Answers1

5

There are several odd corner cases when it comes to default arguments on function parameters.

Here is a presentation from CppCon 2017 detailing a lot of tricky behavior. https://youtu.be/NeJ85q1qddQ

To summarize the main points:

  • Redeclarations cause potentially confusing behavior
  • The default arguments used in virtual function calls are not determined by the function called, but by the static type.
  • Default arguments on function templates can potentially look ill-formed, but may compile as long as they are not instantiated.

Of course, for your case of a non-template constructor, they are fairly innocuous. It can’t be overridden or redeclared (although an out-of-line definition might cause you possible pain).

Michael Price
  • 8,088
  • 1
  • 17
  • 24