5

I'm learning 'modern' C++ and I'm having a really hard time discerning the issue with this code.

Ball.h:

#ifndef BALL_H
#define BALL_H
#include <string>
#include <string_view>

namespace ball
{
    class Ball
    {
        std::string _color{};
        double _radius{};

    public:

        Ball() = default;
        Ball(double radius);
        Ball(std::string_view color = "black", double radius = 10);

        void print() const;
    };
}

#endif // !BALL_H

Ball.cpp:

#include "Ball.h"
#include <iostream>
#include <string_view>

namespace ball
{
    Ball::Ball(double radius)
    {
        _color = "black";
        _radius = radius;
    }

    Ball::Ball(std::string_view color, double radius)
    {
        _color = color;
        _radius = radius;
    }

    void Ball::print() const
    {
        std::cout << "Ball: " << std::endl;
        std::cout << "\tcolor: " << _color << std::endl;
        std::cout << "\tradius: " << _radius << std::endl;
    }

}

Why does defining Ball() = default; produce a compiler error (complaining about the constructor that takes two arguments, causing further confusion).

If I omit the default constructor definition entirely the code compiles and functions perfectly fine.

I wish I could be more descriptive, but I'm at a loss here.

1
  • 1
    I can reproduce this only with older MSVC versions. This seems like a clear bug (that is now fixed) in the compiler. Commented Nov 25, 2022 at 6:48

2 Answers 2

6

Edit

It turned out the actual error in the title C2600 is emitted mistakenly. The answer below still holds, but it's not directly relevant to the described issue, please refer to user17732522's answer for more details.


The problem here is that both arguments in the Ball(std::string_view, double) constructor can be omitted (as they got default values) and there is no way for the compiler to distinguish this constructor from Ball() when the client code relies on a constructor without arguments (having such constructors declared on it's own is not an error, the problem comes at function candidate resolution step):

Ball a; // Ball() or Ball(std::string_view, double) with default arguments?

Thus you will have to leave only one of these two constructors for the class to be default-constructible:

class Ball
{
    ...

public:

    Ball(double radius);
    // all good now. This constructor is used to default-construct the class
    Ball(std::string_view color = "black", double radius = 10);

    ...
};
7
  • This makes a lot of sense. Thanks for the answer!
    – Mimikyutwo
    Commented Nov 24, 2022 at 21:27
  • It is true that any attempt use the default constructor will be ambiguous, but as long as we don't try that I don't see why the program should be ill-formed. Ball::Ball(std::string_view color, double radius) seems to me clearly defining the third overload, not the first one. The actual parameter types should be compared for that determination. Commented Nov 25, 2022 at 4:59
  • @user17732522 I didn't mean the program is ill-formed because of the definition. Having such constructors declared on it's own without having the class default-constructed works just fine in GCC, Clang and MSVC compilers, so you can deduce that OP actually happened to default-construct his/her class to face this issue. when the client code relies on a constructor without arguments is the key here (so i highlighted this part in my answer now) Commented Nov 25, 2022 at 6:47
  • @TheDreamsWind It reproduces without the variable in older MSVC versions. (v19.31 and below) Commented Nov 25, 2022 at 6:49
  • 1
    @TheDreamsWind godbolt.org/z/613bMT5eG That seems to be the error message OP is referring to. It is specifically an issue with matching of the out-of-class definition. Commented Nov 25, 2022 at 6:54
1

The particular error message you are referencing seems to be generated only by older MSVC versions (<= v19.31).

It clearly looks like a bug in the compiler to me that has been fixed in later versions. When determining which in-class declaration the out-of-class definition matches, the parameter types should be compared for equivalency irregardless of default arguments. Therefore your definition matches the overload with two parameters, not the defaulted one without any.

However, the constructors are not useful with this overload set, as pointed out by the other answer. It will fail as soon as you try to default-construct an object because both overloads would be equally good fits for the initialization in overload resolution, resulting in ambiguity.

3
  • @Mimikyutwo you may want to give the accepted answer reward to this answer instead, since it's in fact much more relevant in this case. Commented Nov 25, 2022 at 7:11
  • @TheDreamsWind I've done so for the purposes of this particular question, but I'd like you to know your answer explained what was going on really well, and that was the knowledge I was seeking personally. Thank you again!
    – Mimikyutwo
    Commented Nov 25, 2022 at 17:52
  • Thanks for the time you took on this @user17732522
    – Mimikyutwo
    Commented Nov 25, 2022 at 17:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.