[Bug c++/102482] New: Winit-list-lifetime false positive for temporaries with std::initializer_list
federico.kircheis at gmail dot com
gcc-bugzilla@gcc.gnu.org
Sat Sep 25 12:38:51 GMT 2021
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.
More information about the Gcc-bugs
mailing list