Skip to content

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

@666kod666

Description

@666kod666

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:

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

#include <functional>

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

        std::reference_wrapper<A> m_a;
        std::reference_wrapper<U> 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 <typename U> B(A&, U&) -> B<U>;

    void fun()
    {
        int a = 1;

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

int main()
{
    A<long> 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)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++20clang:frontendLanguage frontend issues, e.g. anything involving "Sema"needs-reductionLarge reproducer that should be reduced into a simpler form

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions