public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108195] New: Incorrect implicit conversion when assigning initializer_list to std::vector
@ 2022-12-21 17:48 gcc-bugzilla at al42and dot me
  2022-12-21 17:53 ` [Bug c++/108195] [13 Regression] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: gcc-bugzilla at al42and dot me @ 2022-12-21 17:48 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-01-24 10:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-21 17:48 [Bug c++/108195] New: Incorrect implicit conversion when assigning initializer_list to std::vector gcc-bugzilla at al42and dot me
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

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