https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107343

            Bug ID: 107343
           Summary: GCC accepts ill-formed out of class definition program
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jlame646 at gmail dot com
  Target Milestone: ---

The following ill-formed program is accepted by gcc and msvc. Demo:
https://godbolt.org/z/9PhnbYhb3

```
class complex {

public:
    complex (double r, double i);



    complex (); 


    complex(const complex &c);


    complex add(complex &c);
    void norm1();

private:

    double re, im; 
    double norm;
};

//this is ill-formed as it turned it into default ctor
complex::complex(double r=0.0, double i=0.0){ 
    re = r; im = i; 
    norm1();
};

complex::complex(){
    re=im=0;
}

complex::complex(const complex &c){
    re=c.re;
    im=c.im;
}

void complex::norm1(){
    norm = sqrt(re*re + im*im);
};

complex complex::add(complex &c){
    return complex( re + c.re, im + c.im);
};

int main(){
    complex c1(3,4), c2(1,2);

    return 0;
}
```
According to https://en.cppreference.com/w/cpp/language/default_arguments

> For a member function of a non-template class, the default arguments are 
> allowed on the out-of-class definition, and are combined with the default 
> arguments provided by the declaration inside the class body. If these 
> out-of-class defaults would turn a member function into a default constructor 
> or copy/move (since C++11) constructor/assignment operator, the program is 
> ill-formed.

Reply via email to