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
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."
*** Bug 86403 has been marked as a duplicate of this bug. ***
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 (); }
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.
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.
Fixed for GCC 12.