llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Incorrect (ambiguous) overload resolution when converting initializer list to array

Open miyuki opened this issue 7 years ago • 2 comments

Bugzilla Link 40098
Version trunk
OS Linux
CC @DougGregor,@riccibruno,@zygoloid

Extended Description

Consider the following code:

struct S {
    S(int i=1) {}
} s1, s2;

int f(S (&&r)[2]) { return 100; }
int f(S (&&r)[3]) { return 200; }

int main() {
    int g = f({s1, s2});
    // assert(g == 100);
}

According to [over.ics.rank]/3.1.2: "List-initialization sequence L1 is a better conversion sequence than list-initialization sequence L2 if:

  • ...
  • L1 converts to type “array of N1 T”, L2 converts to type “array of N2 T”, and N1 is smaller than N2"

I.e., the correct behaviour is to call the first overload. Instead Clang rejects the code because it considers the call ambiguous.

miyuki avatar Dec 19 '18 11:12 miyuki

@llvm/issue-subscribers-clang-frontend

Author: Mikhail Maltsev (miyuki)

| | | | --- | --- | | Bugzilla Link | [40098](https://llvm.org/bz40098) | | Version | trunk | | OS | Linux | | CC | @DougGregor,@riccibruno,@zygoloid |

Extended Description

Consider the following code:

struct S {
    S(int i=1) {}
} s1, s2;

int f(S (&&r)[2]) { return 100; }
int f(S (&&r)[3]) { return 200; }

int main() {
    int g = f({s1, s2});
    // assert(g == 100);
}

According to [over.ics.rank]/3.1.2: "List-initialization sequence L1 is a better conversion sequence than list-initialization sequence L2 if:

  • ...
  • L1 converts to type “array of N1 T”, L2 converts to type “array of N2 T”, and N1 is smaller than N2"

I.e., the correct behaviour is to call the first overload. Instead Clang rejects the code because it considers the call ambiguous.

llvmbot avatar Mar 10 '25 18:03 llvmbot

fixed: https://godbolt.org/z/ojqYv798W

shafik avatar Mar 12 '25 18:03 shafik