keelerjr12
keelerjr12

Reputation: 1903

Why the warning for default arguments?

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.

Upvotes: 5

Views: 1913

Answers (1)

Michael Price
Michael Price

Reputation: 8968

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).

Upvotes: 6

Related Questions