Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed Template Deduction in C++20 Designated Initializers #56394

Closed
Tracked by #425
marstaik opened this issue Jul 5, 2022 · 10 comments
Closed
Tracked by #425

Failed Template Deduction in C++20 Designated Initializers #56394

marstaik opened this issue Jul 5, 2022 · 10 comments
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate

Comments

@marstaik
Copy link

marstaik commented Jul 5, 2022

Tested with clang 14.0

When a template field is initialized via designated initializers, clang fails to deduce the final specialization.

Code example:

template<typename T>
struct Foo {
    int foo;
    T bar;
};

int main() {
    constexpr auto foo = Foo {
        .foo = 1,
        .bar = +[]() { return 5; }
    };

    constexpr auto eval = foo.bar();
    
    return 0;
}

Note that this works succesfully in MSVC and GCC:

MSVC: https://godbolt.org/z/qr5a56xbs
GCC: https://godbolt.org/z/sqc4r1Eb4
clang: https://godbolt.org/z/P1c3v7Gfj

The compiler reports:

x86-64 clang 14.0.0 - 358ms

<source>:8:26: error: no viable constructor or deduction guide for deduction of template arguments of 'Foo'
    constexpr auto foo = Foo {
                         ^
<source>:2:8: note: candidate function template not viable: requires 1 argument, but 2 were provided
struct Foo {
       ^
<source>:2:8: note: candidate function template not viable: requires 0 arguments, but 2 were provided
1 error generated.
ASM generation compiler returned: 1
<source>:8:26: error: no viable constructor or deduction guide for deduction of template arguments of 'Foo'
    constexpr auto foo = Foo {
                         ^
<source>:2:8: note: candidate function template not viable: requires 1 argument, but 2 were provided
struct Foo {
       ^
<source>:2:8: note: candidate function template not viable: requires 0 arguments, but 2 were provided
1 error generated.
Execution build compiler returned: 1
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 5, 2022

@llvm/issue-subscribers-clang-format

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 5, 2022

@llvm/issue-subscribers-c-20

@marstaik
Copy link
Author

marstaik commented Jul 5, 2022

@EugeneZelenko I think this is more of a clang-frontend issue than a clang-format issue :P

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang-format labels Jul 5, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 5, 2022

@llvm/issue-subscribers-clang-frontend

@EugeneZelenko
Copy link
Contributor

@marstaik: Sorry for my mistake. Fixed.

@schwarzschild-radius
Copy link
Contributor

I think it is because of missing deduction candidate. Here is a smaller repro (https://godbolt.org/z/b4v14T74d)

template<typename T>
struct Foo {
    int foo;
    T bar;
};

int main() {
    auto foo = Foo {1, 0.5};
    return 0;
}

@alanzhao1 alanzhao1 self-assigned this Aug 10, 2022
@alanzhao1
Copy link
Contributor

Fixing this is going to be somewhat complicated

Currently, template argument deduction is centered around functions. Functions cover things such as constructors. Aggregate initializers, however, do not fit this model, so I think there might be a good amount of refactoring necessary to resolve this.

@danakj
Copy link
Contributor

danakj commented Nov 6, 2022

Even smaller repro: https://godbolt.org/z/xqbde4hfM

template <class T>
struct S {
    T t;
};

S s{.t = int(3)};

@alanzhao1
Copy link
Contributor

I believe this bug is a duplicate of #54050 and #54049, both of which are track the implementation of class template argument deduction for aggregates as described in P1816R0 and P2082R1.

@yuanfang-chen
Copy link
Collaborator

duplicates #54049 and #54050

@EugeneZelenko EugeneZelenko added the duplicate Resolved as duplicate label Jul 5, 2023
@EugeneZelenko EugeneZelenko closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate
Projects
Status: Done
Development

No branches or pull requests

7 participants