From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E9BA53857817; Tue, 5 Jan 2021 08:43:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9BA53857817 From: "adr26__gcc at nunsway dot co.uk" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/98524] New: accepts-invalid instantiation of variable template with different function pointer type from declaration Date: Tue, 05 Jan 2021 08:43:31 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: adr26__gcc at nunsway dot co.uk X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jan 2021 08:43:32 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98524 Bug ID: 98524 Summary: accepts-invalid instantiation of variable template with different function pointer type from declaration Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: adr26__gcc at nunsway dot co.uk Target Milestone: --- Consider: template void (*TF)(int); template void (*TF)(int); Per [temp.explicit]/6, the above are two declarations of the same entity: The declaration in an explicit-instantiation and the declaration produced= by the corresponding substitution into the templated function, variable, or cl= ass are two declarations of the same entity. [Note 1: These declarations are required to have matching types as specif= ied in [basic.link], except as specified in [except.spec]. [Example 2: template T var =3D {}; template float var; // OK, instantiated variable has type f= loat template int var[]; // OK, absence of major array bound is permitted template int *var; // error: instantiated variable has type int template auto av =3D T(); template int av; // OK, variable with type int can be redeclared with type auto template auto f() {} template void f(); // error: function with deduced return = type // redeclared with non-deduced return t= ype ([dcl.spec.auto]) =E2=80=94 end example] =E2=80=94 end note] http://eel.is/c++draft/temp.explicit#6 and per [basic.link]/11.1, these two declarations of a entity which is a variable shall declare the variable to be of the same (function pointer) ty= pe: For any two declarations of an entity E : (11.1) If one declares E to be a variable or function, the other shall declare E as one of the same type. http://eel.is/c++draft/basic.link#11.1 There isn't a "no diagnostic required" exception for this case in either [temp.explicit] or [basic.link], and clang and ICC (EDG front end) both correctly detect this error: clang: :2:10: error: type 'void (*)(int)' of explicit instantiation of '= TF' does not match expected type 'void (*)()' template void (*TF)(int); ^ :1:29: note: variable template 'TF' declared here template void (*TF)(); ^ ICC: (2): error: declaration is incompatible with "void (*TF)() [with T=3Dint]" (declared at line 1) template void (*TF)(int); ^ https://gcc.godbolt.org/z/qa6395 [NB: MSVC also incorrectly fails to detect this error, and I have reported = this in the context of a separate bug in its handling of variable templates: https://developercommunity2.visualstudio.com/t/Incorrect-C3410-on-explicit-= instantiatio/1301005 where it doesn't handle the correct code: template void (*TF)(int); template void (*TF)(int); !] g++ should issue a diagnostic for this, similarly to clang and ICC/EDG. Thanks, Andrew R=