6

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.

5
  • 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. Commented Jul 11, 2018 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! Commented Jul 11, 2018 at 18:16
  • 2
    Related: stackoverflow.com/questions/51217635/… Commented Jul 11, 2018 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. Commented Jul 11, 2018 at 18:47
  • Recommend removing the c++17 tag as this behavior is not changed in that version of the standard. Commented Jul 12, 2018 at 4:25

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.