From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 35A9F3858D3C; Sun, 3 Oct 2021 04:04:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 35A9F3858D3C From: "13508417 at qq dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/102573] New: optimized code removes the underlying elements of the std::initializer_list being copied Date: Sun, 03 Oct 2021 04:04:20 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 4.8.5 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: 13508417 at qq dot com 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: Sun, 03 Oct 2021 04:04:20 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D102573 Bug ID: 102573 Summary: optimized code removes the underlying elements of the std::initializer_list being copied Product: gcc Version: 4.8.5 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: 13508417 at qq dot com Target Milestone: --- Using clang++ compiler with libstdc++, the output of below program differs whether use -O option. ---- code: #include #include using namespace std; int main() { initializer_list il; il =3D {111, 222}; cout << "initializer_list size: " << il.size() << endl; cout << "initializer_list underlying elements: " << endl; for (auto i : il) cout << i << endl; return 0; } ---- command: =E2=9E=9C clang++ test_2.cpp -std=3Dc++11 -O1 -v clang version 10.0.0 Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/bin Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.8.2 Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.8.5 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2 Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5 Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5 ... ---- output without -O: initializer_list size: 2 initializer_list underlying elements: 111 222 ---- output with -O1: initializer_list size: 2 initializer_list underlying elements: 1402476368 32767 ---- Question: >>From https://timsong-cpp.github.io/cppwp/n4659/dcl.init.list, I know that t= he reason is because the lifetime of the underlying array should be same as the temporary initializer_list constructed directly from the "{111, 222}", and = the variable "il" only copy the address and length of the underlying array using the default copy member, but cannot affect the lifetime of the underlying array. But my question is, since the copied initializer_list can't affect the life= time of the underlying array bound to the original initializer_list, can we just prohibit the copy member in the library implementation of std::initializer_list? It seems to me that the copy member makes not much sense, but probably lures the user to make mistakes.=