3

Is the following program ill-formed?

struct Foo
{
    Foo(Foo&) = default;

    Foo(const Foo&) = default;
};

int main() 
{
}

It successfully compiles with clang++ 3.8.0 and g++ 6.3.0 (compiler flags are -std=c++11 -Wall -Wextra -Werror -pedantic-errors).

Constructor
  • 7,273
  • 2
  • 24
  • 66
  • 1
    Um.. These are not *default* copy constructors. These are *default**ed*** copy constructors. Other than that they are no different from copy constructors defined in regular fashion (with `{}`). There's nothing unusual in having two copy constructors with different const-qualifications on the parameter. – AnT stands with Russia Apr 10 '17 at 08:14
  • @AnT I'm sorry for my bad English. Default**ed**, of course. – Constructor Apr 10 '17 at 08:16
  • `Foo(Foo&)` is (also) a valid copy constructor. – Jarod42 Apr 10 '17 at 08:17
  • @Jarod42 Yes, I know. My question is about having two defaulted copy constructors (with different signatures) simultaneously. – Constructor Apr 10 '17 at 08:18

1 Answers1

7

Why should this be ill-formed? You define two copy constructors, one which expects a non-const argument and the other which can use a const argument. You then tell the compiler it should use its default implementation for these two constructors. Unless the compiler has a reason to eliminate the default copy constructors, you could also delete those two lines and would get the same result. Also I think the first version is redundant, since the default implementation should anyway be fine with a const argument. Still defining both is legal, since you might want to do something different in the two cases.