public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "de34 at live dot cn" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-107092-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107092

            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.integral], [class.conv]).

It's obvious that BadFrom can't be converted to any integral type.

#include <algorithm>
#include <concepts>

struct BadFrom {
    template<std::same_as<double> 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" requirement is
defective (as ambiguity is not considered at all), I think libstdc++ should
reject using BadFrom as Size.

             reply	other threads:[~2022-09-30  3:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-30  3:20 de34 at live dot cn [this message]
2022-09-30 10:54 ` [Bug libstdc++/107092] " redi at gcc dot gnu.org
2022-09-30 10:56 ` redi at gcc dot gnu.org
2022-10-14  2:29 ` de34 at live dot cn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107092-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).