From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 04A663858D35; Mon, 26 Dec 2022 04:19:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 04A663858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672028385; bh=a2hsoQ6i0zkLeENtDuD35+0e2ItHtVeGyrvgZ2Qw+EY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Sf2OFvNW+A+aMB6VaBSqybZQBwou3crcmqUhr9/3h+2YI4bRDgT64wau8K6hVmmM4 Ij6OmJagwA35FmRSyAcYw7P/GNcMc3yrDX36+flb1qbVpRN0HV1pjIuRymcyxfy6ai ybkqn8dTQ/OiYnUPQmlo9NdyrKLDBj25bPdfw6UI= From: "mail at jhellings dot nl" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/104577] needs copy constructor for class non-type template parameter Date: Mon, 26 Dec 2022 04:19:43 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: mail at jhellings dot nl X-Bugzilla-Status: NEW 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: cc Message-ID: In-Reply-To: References: 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104577 mail at jhellings dot nl changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mail at jhellings dot nl --- Comment #2 from mail at jhellings dot nl --- I looked a bit further into this and into what the standard says. GCC does partially the correct thing in this case, whereas several other compilers do the wrong thing. See https://jhellings.nl/article?articleid=3D1 for the full analysis. The short summary: In Clause 8 of Section [temp.param], the standard defines the value of a non-type template argument: "An id-expression naming a non-type template-parameter of class type T deno= tes a static storage duration object of type const T known as a template parame= ter object, whose value is that of the corresponding template argument after it= has been converted to the type of the template-parameter. ..." Hence, whatever is provided as a non-type template parameter argument (of t= ype S in this bug report) is converted to the type S and the value resulting fr= om this conversion is available within the template as an lvalue object of type const S. To convert an expression to type S, you either need a constexpr copy constructor (general case) or a constexpr move constructor (in the special = case in which you provide a movable value).=20 Note that both Clang and Microsoft C++ do not correctly implement the seman= tics of non-type template parameters (they pass values without converting them to the type of the non-type template parameter). I did find a separate issue, however: /* * @author{Jelle Hellings}. * @copyright{The 2-Clause BSD License; see the end of this article}. */ /* * A type that can only be default-constructed and moved. */ struct no_copy { /* * We can default-construct a dummy. */ constexpr no_copy() {}; /* * We cannot copy dummy. */ no_copy(const no_copy&) =3D delete; /* * But we certainly can move a dummy. */ constexpr no_copy(no_copy&&) {} }; /* * A template function that accepts a no_copy non-type template parameter, = but * does not do anything with it. */ template=20 void test_f() { /* We cannot pass NC to another template, as we do not have a copy * constructor. We can use this template by moving in a no_copy, howeve= r. */ }; /* * A template struct that accepts a no_copy non-type template parameter, but * does not do anything with it. */ template=20 struct test_t { /* We cannot pass NC to another template, as we do not have a copy * constructor. We can use this template by moving in a no_copy, howeve= r. */ }; /* * Entry-point of the program. */ int main () { test_f(); // Works fine, as it should. test_t value; // <- error: use of deleted function. }=