The default argument argument is replaced when re-declaring

Why is the following code well-formed:

void foo(int i, int j = 56);
void foo(int i = 42, int j);

int main(){  }

Demo

But the following

void foo(int i = 42, int j);
void foo(int i, int j = 56);

int main(){  }

Demo

badly formed. I tried to find in N4296::8.3.6 [dcl.fct.default], and I found the following example:

class C 
{
    void f(int i = 3);
    void g(int i, int j = 99);
};
void C::f(int i = 3) { } //error
void C::g(int i = 88, int j) { // C::g can be called with no argument
}

But clang doesn't seem like that.

struct A
{
    void foo(int i = 42, int j);
};

void A::foo(int i, int j = 56){ }; //error

int main(){  }

Demo

So, is this an implementation issue? Formally, this whole example should be acceptable if they are?

+4
source share
1 answer

[dcl.fct.default]

  1. [...] default arguments can be added to later function declarations in the same scope.
void foo(int i, int j = 56);
void foo(int i = 42, int j);

This is great because the second declaration adds a default argument to the first parameter that previously did not have this.

[...] , , , [...]

, , .

void foo(int i = 42, int j); // error
void foo(int i, int j = 56);

, , , .

struct A
{
    void foo(int i = 42, int j); // the error should be here
};

void A::foo(int i, int j = 56){ }; // not here

, , j , .

+1

Source: https://habr.com/ru/post/1570257/


All Articles