Issue 99845
Summary designated initializer of template class rejected by clang
Labels clang
Assignees
Reporter Rush10233
    The following code is rejected by clang but accepted by gcc and MSVC with `-std=c++20`:

```c++
template <typename T>
struct Foo {
    T x;
};

template <typename T> auto tst(){
    auto foo=Foo{.x=0};
    //auto foo=Foo{0};
    return foo;
}

int main()
{
    auto res=tst<int>();
}
```

It seems that in the instantiation of `foo`, the template argument `T` of `Foo<T>` could be deduced to `int` according to the type of `Foo::x`, while clang complains about the deduction failure of `T`:

```c++
<source>:7:14: error: no viable constructor or deduction guide for deduction of template arguments of 'Foo'
    7 |     auto foo=Foo{.x=0};
      | ^
<source>:14:14: note: in instantiation of function template specialization 'tst<int>' requested here
   14 |     auto res=tst<int>();
      |              ^
<source>:2:8: note: candidate template ignored: couldn't infer template argument 'T'
    2 | struct Foo {
      |        ^
<source>:2:8: note: implicit deduction guide declared as 'template <typename T> Foo(int) -> Foo<T>'
<source>:2:8: note: candidate template ignored: could not match 'Foo<T>' against 'int'
<source>:2:8: note: implicit deduction guide declared as 'template <typename T> Foo(Foo<T>) -> Foo<T>'
<source>:2:8: note: candidate function template not viable: requires 0 arguments, but 1 was provided
<source>:2:8: note: implicit deduction guide declared as 'template <typename T> Foo() -> Foo<T>'
1 error generated.
```

It's also worth noting that using `auto foo=Foo{0}` does not trigger any error.

[https://godbolt.org/z/d3ccWsjWa](https://godbolt.org/z/d3ccWsjWa)
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to