public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102482] New: Winit-list-lifetime false positive for temporaries with std::initializer_list
@ 2021-09-25 12:38 federico.kircheis at gmail dot com
  2021-09-26 11:08 ` [Bug c++/102482] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: federico.kircheis at gmail dot com @ 2021-09-25 12:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102482
           Summary: Winit-list-lifetime false positive for temporaries
                    with std::initializer_list
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.kircheis at gmail dot com
  Target Milestone: ---

Following program


----
#include <initializer_list>

struct span {
    span(std::initializer_list<int> il) noexcept : begin(il.begin()),
size(il.size()) {}
    const int* begin;
    std::size_t size;
};


int foo(span sp){
        return sp.begin[0];
}

int main() {
    return foo(span(std::initializer_list<int>{}));
}
----


triggers

warning: initializing 'span::begin' from 'std::initializer_list<int>::begin'
does not extend the lifetime of the underlying array [-Winit-list-lifetime]

with no compiler flags except "--std=c++20"

(https://godbolt.org/z/ocaxh46oW)


While the warning is true, the code is completely safe, but the warnings seems
to imply that foo will access a dangling pointer.

Strangely, it does not diagnose anything on


----
#include <vector>

struct span {
    span(std::vector<int> il) noexcept : begin(il.data()), size(il.size()) {}
    const int* begin;
    std::size_t size;
};


int foo(span sp){
        return sp.begin[0];
}

int main() {
    return foo(span(std::vector<int>{}));
}
----

(https://godbolt.org/z/raMoTohvc)

where the lifetime rules are the same.

so I assume the warning is std::initializer_list specific.

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

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

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-25 12:38 [Bug c++/102482] New: Winit-list-lifetime false positive for temporaries with std::initializer_list federico.kircheis at gmail dot com
2021-09-26 11:08 ` [Bug c++/102482] " redi at gcc dot gnu.org
2021-09-26 11:08 ` redi at gcc dot gnu.org
2021-09-26 11:10 ` redi at gcc dot gnu.org
2021-09-29  5:39 ` federico.kircheis at gmail dot com
2021-09-29  9:23 ` redi at gcc dot gnu.org
2021-09-29  9:25 ` redi at gcc dot gnu.org
2021-09-29 10:16 ` redi at gcc dot gnu.org
2021-10-04 12:22 ` redi at gcc dot gnu.org
2021-10-07 13:55 ` cvs-commit 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).