llvm / llvm/llvm-project

[CTAD] Generated implicit deduction guides fails for inner / nested class when ctor has ref to outer class

Open
#204,017 7 comments 0 reactions 0 assignees View on GitHub
c++20 clang:frontend confirmed
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The generated implicit deduction guide is invalid for nested (inner) class template where:
* C++20+ standard is used.
* outer class is also class template.
* constructor matching invocation contains reference to outer class as parameter (in short form).

Sample:
```cpp
// clang/gcc: -std=c++20 -Wall -O2 -pedantic
// msvc: -std:c++20 -W4 -O2

#include

template
struct A
{
template
struct B
{
B(A& a, U& b)
: m_a(std::ref(a)), m_b(std::ref(b))
{}

std::reference_wrapper m_a;
std::reference_wrapper m_b;
};

// Generated implicit deduction is invalid and in more
// complicated scenarios causes compiler to crash.
// The implicit guide is correctly generated on
// msvc and gcc.
//
// If you uncomment this explicit guide it will work:
// template B(A&, U&) -> B;

void fun()
{
int a = 1;

B b1{*this, a};
}
};

int main()
{
A a;
a.fun();
}

```

For more sophisticated deduction the invalid CTAD / generated deduction guide can cause compiler crash (SIGSEGV on Linux, Access Violation on Windows (`clang-cl`)).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.