public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gcc-bugzilla at al42and dot me" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/108195] New: Incorrect implicit conversion when assigning initializer_list to std::vector
Date: Wed, 21 Dec 2022 17:48:15 +0000	[thread overview]
Message-ID: <bug-108195-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108195
           Summary: Incorrect implicit conversion when assigning
                    initializer_list to std::vector
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc-bugzilla at al42and dot me
  Target Milestone: ---

The following code compiles fine with Clang 15 and GCC 12 and outputs "3" when
run.

With GCC 13, it produces a warning about narrowing conversion and constructs a
vector of length 2.

$ cat test.cpp 
#include <iostream>
#include <vector>

struct S
{
    S(bool) {}
};

int main()
{
    std::vector<S> v = { true, false, true };
    std::cout << v.size() << std::endl;
}

$ g++ -std=c++17 test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:11:44: warning: narrowing conversion of ‘(((void)const bool [3]{true,
false, true}), ((const bool*)(&<anonymous>)))’ from ‘const bool*’ to ‘bool’
[-Wnarrowing]
   11 |     std::vector<S> v = { true, false, true };
      |                                            ^
test.cpp:11:44: warning: narrowing conversion of ‘(((const
bool*)(&<anonymous>)) + 3)’ from ‘const bool*’ to ‘bool’ [-Wnarrowing]

$ ./test
2

Using a constructor instead of the assignment avoids this problem:
std::vector<S> v { true, false, true }; // works fine

Creating an initializer_list separately is also ok:
std::initializer_list<S> il = { true, false, true };
std::vector<S>           v  = il; // no problem here, vector has three elements

Tested with GCC fdc7469cf597ec11229ddfc3e9c7a06f3d0fba9d. Bisection points to
d081807d8d70e3e87eae41e1560e54d503f4d465 (PR105838).

             reply	other threads:[~2022-12-21 17:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-21 17:48 gcc-bugzilla at al42and dot me [this message]
2022-12-21 17:53 ` [Bug c++/108195] [13 Regression] " pinskia at gcc dot gnu.org
2022-12-21 17:57 ` pinskia at gcc dot gnu.org
2022-12-21 17:59 ` pinskia at gcc dot gnu.org
2022-12-22  7:19 ` [Bug c++/108195] [13 Regression] Incorrect implicit conversion when assigning initializer_list to std::vector since r13-4564-gd081807d8d70e3 rguenth at gcc dot gnu.org
2023-01-23 19:12 ` jason at gcc dot gnu.org
2023-01-23 21:13 ` cvs-commit at gcc dot gnu.org
2023-01-23 21:13 ` jason at gcc dot gnu.org
2023-01-24 10:02 ` redi at gcc dot gnu.org

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-108195-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).