public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/102573] New: optimized code removes the underlying elements of the std::initializer_list being copied
@ 2021-10-03  4:04 13508417 at qq dot com
  2021-10-03  4:09 ` [Bug libstdc++/102573] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: 13508417 at qq dot com @ 2021-10-03  4:04 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102573
           Summary: optimized code removes the underlying elements of the
                    std::initializer_list being copied
           Product: gcc
           Version: 4.8.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 13508417 at qq dot com
  Target Milestone: ---

Using clang++ compiler with libstdc++, the output of below program differs
whether use -O option.

----

code:
#include<iostream>
#include<initializer_list>
using namespace std;

int main()
{
    initializer_list<int> il;
    il = {111, 222};

    cout << "initializer_list size: " << il.size() << endl;
    cout << "initializer_list underlying elements: " << endl;
    for (auto i : il)
        cout << i << endl;

    return 0;
}

----

command:
➜  clang++ test_2.cpp -std=c++11 -O1 -v
clang version 10.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/i686-redhat-linux/4.8.5
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.8.5
...

----

output without -O:
initializer_list size: 2
initializer_list underlying elements:
111
222

----

output with -O1:
initializer_list size: 2
initializer_list underlying elements:
1402476368
32767

----

Question:
>From https://timsong-cpp.github.io/cppwp/n4659/dcl.init.list, I know that the
reason is because the lifetime of the underlying array should be same as the
temporary initializer_list constructed directly from the "{111, 222}", and the
variable "il" only copy the address and length of the underlying array using
the default copy member, but cannot affect the lifetime of the underlying
array.

But my question is, since the copied initializer_list can't affect the lifetime
of the underlying array bound to the original initializer_list, can we just
prohibit the copy member in the library implementation of
std::initializer_list? It seems to me that the copy member makes not much
sense, but probably lures the user to make mistakes.

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

end of thread, other threads:[~2021-10-04  6:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-03  4:04 [Bug libstdc++/102573] New: optimized code removes the underlying elements of the std::initializer_list being copied 13508417 at qq dot com
2021-10-03  4:09 ` [Bug libstdc++/102573] " pinskia at gcc dot gnu.org
2021-10-03  5:49 ` 13508417 at qq dot com
2021-10-03  6:20 ` pinskia at gcc dot gnu.org
2021-10-03 14:16 ` 13508417 at qq dot com
2021-10-04  6:55 ` 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).