Bug 79501 - member deduction guide not recognized
Summary: member deduction guide not recognized
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: 12.0
Assignee: Patrick Palka
URL:
Keywords: rejects-valid
: 86403 (view as bug list)
Depends on:
Blocks: 105655
  Show dependency treegraph
 
Reported: 2017-02-14 02:26 UTC by Richard Smith
Modified: 2022-05-25 18:50 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-03-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Smith 2017-02-14 02:26:26 UTC
struct X {
  template<typename T> struct A {};
  A() -> A<int>;
};

... results in:

<source>:3:10: error: ISO C++ forbids declaration of 'A' with no type [-fpermissive]
  A() -> A<int>;
         ^~~~~~
<source>:3:10: error: 'A' function with trailing return type not declared with 'auto' type specifier


... and ...

struct X {
  template<typename T> struct A {};
  template<typename T> A() -> A<int>;
};

... results in:

<source>:3:24: error: deduction guide 'X::A() -> X::A<int>' must be declared at namespace scope
Comment 1 eracpp 2018-07-23 21:42:28 UTC
Still observed on trunk (GCC 9.0.0 20180722):
https://wandbox.org/permlink/vpY98XCvEz2k0IN9

Relevant paragraph from the standard ([temp.deduct.guild]-3):
http://eel.is/c++draft/temp.deduct.guide#3.sentence-4

"A deduction-guide shall be declared in the same scope as the corresponding class template and, for a member class template, with the same access."
Comment 2 Marek Polacek 2020-09-16 20:03:01 UTC
*** Bug 86403 has been marked as a duplicate of this bug. ***
Comment 3 Marek Polacek 2020-09-16 20:32:21 UTC
Expanded test for the second problem:

// PR c++/79501
// { dg-do compile { target c++17 } }

struct X {
  template<typename T>
  struct B { T t; };

  // In C++20, should work even without this guide (P1816).
  template<typename T> B(T) -> B<T>;

  void foo () {
    B{1};
  }
};

void
fn ()
{
  X x;
  x.foo ();
}
Comment 4 GCC Commits 2021-07-12 20:35:40 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:8d75b8830e9dafb4e0c400c723653512adf40295

commit r12-2260-g8d75b8830e9dafb4e0c400c723653512adf40295
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Jul 12 16:35:18 2021 -0400

    c++: permit deduction guides at class scope [PR79501]
    
    This adds support for declaring (class-scope) deduction guides for a
    member class template.  Fortunately it seems only a couple of changes
    are needed in order for the existing CTAD machinery to handle them
    properly: we need to make sure to give them a FUNCTION_TYPE instead of a
    METHOD_TYPE, and we need to avoid using a BASELINK when looking them up.
    
            PR c++/79501
            PR c++/100983
    
    gcc/cp/ChangeLog:
    
            * decl.c (grokfndecl): Don't require that deduction guides are
            declared at namespace scope.  Check that class-scope deduction
            guides have the same access as the member class template.
            (grokdeclarator): Pretend class-scope deduction guides are static.
            * search.c (lookup_member): Don't use a BASELINK for (class-scope)
            deduction guides.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1z/class-deduction92.C: New test.
            * g++.dg/cpp1z/class-deduction93.C: New test.
            * g++.dg/cpp1z/class-deduction94.C: New test.
            * g++.dg/cpp1z/class-deduction95.C: New test.
Comment 5 GCC Commits 2021-08-11 20:00:00 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:7e39d1a15f5276f72ee478a692445569bb646e65

commit r12-2860-g7e39d1a15f5276f72ee478a692445569bb646e65
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Aug 11 15:59:22 2021 -0400

    c++: recognize class-scope non-template dguides [PR79501]
    
    It looks like we still don't recognize class-scope non-template
    deduction guides even after r12-2260.   This is because deduction guides
    are tagged as such in cp_parser_init_declarator after calling
    cp_parser_declarator, but in cp_parser_member_declaration we call
    cp_parser_declarator directly.
    
    So let's tag them in cp_parser_member_declaration as well.
    
            PR c++/79501
    
    gcc/cp/ChangeLog:
    
            * parser.c (maybe_adjust_declarator_for_dguide): New, split
            out from ...
            (cp_parser_init_declarator): ... here.
            (cp_parser_member_declaration): Use it.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1z/class-deduction98.C: New test.
Comment 6 Patrick Palka 2021-08-11 20:05:44 UTC
Fixed for GCC 12.