public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114123] New: list-initialization with a single element
@ 2024-02-27 3:56 yx_liu at hotmail dot com
2024-02-27 4:04 ` [Bug c++/114123] " pinskia at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: yx_liu at hotmail dot com @ 2024-02-27 3:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114123
Bug ID: 114123
Summary: list-initialization with a single element
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: yx_liu at hotmail dot com
Target Milestone: ---
https://godbolt.org/z/fPd4q7zMd
The issue happens with gcc 13.2 and trunk. The following is the code:
```
#include <vector>
#include <stdio.h>
using namespace std;
struct A {
int x;
A(int x_) : x(x_) {printf("%p : A(int %d)\n", this, x);}
A(const A& a) { x = a.x; printf("%p : A(const A& %p)\n", this, &a);}
A(const vector<A>& a) { printf ("%p : vector<A>& %p\n", this, &a);}
};
int main() {
vector<A> a{1,2};
vector<A> b{a};
printf("%ld\n", b.size());
}
```
Based on my understanding of https://cplusplus.github.io/CWG/issues/1467.html,
If T is a class type and the initializer list has a single element of type cv
U, where U is T or a class derived from T, the object is initialized from that
element (by copy-initialization for copy-list-initialization, or by
direct-initialization for direct-list-initialization).
b should be direct-initialized by a, i.e. equivalent to
vector<A> b(a);
I would expect the copy ctor of vector<A> to be called, and the elements of a
will be copied to b, and b.size() will be 2.
However, A(const vector<A>& a) is called to contruct b and b.size() is 1.
Currently clang trunk has the expected behavior and b.size() is 1.
https://godbolt.org/z/dh7d5x81T
A few days ago when clang tried to implement CWG2137 (
https://github.com/llvm/llvm-project/pull/77768). It showed the same behavior
as gcc. However that PR was reverted and clang went back to the expected
behavior.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/114123] list-initialization with a single element
2024-02-27 3:56 [Bug c++/114123] New: list-initialization with a single element yx_liu at hotmail dot com
@ 2024-02-27 4:04 ` pinskia at gcc dot gnu.org
2024-02-27 4:16 ` pinskia at gcc dot gnu.org
2024-02-27 5:02 ` yx_liu at hotmail dot com
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-27 4:04 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114123
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think GCC's behavior here is correct, see PR 83264 .
Specifically https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83264#c11 .
And https://cplusplus.github.io/CWG/issues/1467.html .
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/114123] list-initialization with a single element
2024-02-27 3:56 [Bug c++/114123] New: list-initialization with a single element yx_liu at hotmail dot com
2024-02-27 4:04 ` [Bug c++/114123] " pinskia at gcc dot gnu.org
@ 2024-02-27 4:16 ` pinskia at gcc dot gnu.org
2024-02-27 5:02 ` yx_liu at hotmail dot com
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-27 4:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114123
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83264#c14 where Jason is
pinging the CWG about the interactions here.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/114123] list-initialization with a single element
2024-02-27 3:56 [Bug c++/114123] New: list-initialization with a single element yx_liu at hotmail dot com
2024-02-27 4:04 ` [Bug c++/114123] " pinskia at gcc dot gnu.org
2024-02-27 4:16 ` pinskia at gcc dot gnu.org
@ 2024-02-27 5:02 ` yx_liu at hotmail dot com
2 siblings, 0 replies; 4+ messages in thread
From: yx_liu at hotmail dot com @ 2024-02-27 5:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114123
--- Comment #3 from Yaxun Liu <yx_liu at hotmail dot com> ---
So, since vector<A> has a ctor that accepts initializer list, that ctor is
favored over its copy ctor. With the initializer-list ctor, a is converted to
A(a) first, then {A(a)} is passed to that ctor.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-02-27 5:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-27 3:56 [Bug c++/114123] New: list-initialization with a single element yx_liu at hotmail dot com
2024-02-27 4:04 ` [Bug c++/114123] " pinskia at gcc dot gnu.org
2024-02-27 4:16 ` pinskia at gcc dot gnu.org
2024-02-27 5:02 ` yx_liu at hotmail dot com
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).