Skip to content

Aggregate deduction broken in function templates #140596

Open
@snarkmaster

Description

@snarkmaster

The following snippet works on MSVC and GCC, but fails to compile fnTemplate under Clang -- https://godbolt.org/z/KvjTqarxa:

struct Inner {
  // Clang works if you comment this
  Inner() = default;
};

template <typename T>
struct WantsCTAD {
  T t_;
};

auto fn() {
    WantsCTAD ok{.t_ = Inner{}};
    (void)ok;
}

template <typename>
void fnTemplate() {
    // error: no viable constructor or deduction guide for deduction of template arguments of 'WantsCTAD')
    WantsCTAD bad{.t_ = Inner{}};
    (void)bad;
}

int main() {
  fn();
  fnTemplate<int>();
  return 0;
}

I'm a bad language lawyer, so I can't say with confidence whether Clang or GCC/MSVC are out of compliance here, but ... it's surprising and inconsistent that this behavior is contingent on both of these:

  • asking for CTAD in a function template (rather than just function)
  • adding a user-declared ctor to Inner -- IIUC as of C++20 this makes Inner not an aggregate type

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions