[Bug c++/48562] [C++0x] warn about uses of initializer_list that will lead to dangling pointers
fw at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Mar 28 11:44:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48562
Florian Weimer <fw at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |fw at gcc dot gnu.org
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=66476
--- Comment #8 from Florian Weimer <fw at gcc dot gnu.org> ---
It would also mention to warn about std::initializer_list references in
function arguments, I think. We received a downstream bug report:
#include <initializer_list>
#include <iostream>
template <typename T> class ArrayRef {
public:
using size_type = size_t;
private:
/// The start of the array, in an external buffer.
const T *Data = nullptr;
/// The number of elements.
size_type Length = 0;
public:
/// Construct an ArrayRef from a std::initializer_list.
/*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
: Data(Vec.begin() == Vec.end() ? (T *)nullptr : Vec.begin()),
Length(Vec.size()) {}
const T &operator[](size_t Index) const { return Data[Index]; }
};
int main(int argc, char **argv) {
const ArrayRef<int> Foo = {42};
std::cout << "Foo " << Foo[0] << "\n";
return 0;
}
https://bugzilla.redhat.com/show_bug.cgi?id=1561373
I believe this code is buggy, and it would be nice to warn about this. Almost
any std::initializer_list object will be a temporary, after all, and the called
function should move the initializer elements, not copy them.
More information about the Gcc-bugs
mailing list