From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 0B85D3858D38; Fri, 30 Sep 2022 03:20:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B85D3858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664508017; bh=N8tZfLGT2mBS/Com5WRTICe9qC35DN9N7kPKpppwYhk=; h=From:To:Subject:Date:From; b=JZf48TPtM6pnaiT40olQGicpfqvTLDags3t1OaRhhO6vBAkvBoL1x74HrDaHfURl3 AC/kC1ORpygnNwNz0YO5IJWv6VVQ4z95PQ5a1maOzVVjy9auJZQjjS575wQIjgbTAP 0HpCDdOVl1VXm5za9T+XUyGL3LUekG9May++zYZo= From: "de34 at live dot cn" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/107092] New: std::for_each_n and its friends incorrectly accept size parameters that are not convertible to an integer type Date: Fri, 30 Sep 2022 03:20:16 +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: 12.1.0 X-Bugzilla-Keywords: accepts-invalid X-Bugzilla-Severity: normal X-Bugzilla-Who: de34 at live dot cn 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 keywords 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107092 Bug ID: 107092 Summary: std::for_each_n and its friends incorrectly accept size parameters that are not convertible to an integer type Product: gcc Version: 12.1.0 Status: UNCONFIRMED Keywords: accepts-invalid Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: de34 at live dot cn Target Milestone: --- This program shouldn't compile due to [alg.foreach]/16: > Mandates: The type Size is convertible to an integral type ([conv.integra= l], [class.conv]). It's obvious that BadFrom can't be converted to any integral type. #include #include struct BadFrom { template D> operator D() const { return 42.0; } }; int main() { int arr[42]{}; std::for_each_n(arr, BadFrom{}, [](int){}); } However, this is accepted by libstdc++, because the conversion is done by __size_to_integer overloads in stl_algobase.h (added via PR 87982), and overloads for floating-point types are (probably) incorrectly provided. Godbolt link: https://godbolt.org/z/EKvx7dY4e While I believe the current "is convertible to an integral type" requiremen= t is defective (as ambiguity is not considered at all), I think libstdc++ should reject using BadFrom as Size.=