This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/48562] [C++0x] warn about uses of initializer_list that will lead to dangling pointers


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48562

--- Comment #6 from Johannes Schaub <schaub.johannes at googlemail dot com> 2011-09-25 14:22:33 UTC ---
(In reply to comment #5)
> Johannes, sorry about the dumb question: now I understand the issue decently
> well - and after all boils down to adding a warning - but I'm not sure to
> understand your code snippet: is it meant to crash at runtime? Trigger valgrind
> errors?

In the C++11 spec, it is said that the lifetime of the backing-up array is the
same as the lifetime of the initializer_list object which was initialized by
the array (not considering the DRs and their resolution that Jason has pointed
to). My code was just meant to test whether GCC obeys those rules.

struct X {
  X(int) { cout << "+"; }
  X(X const&) { cout << "+"; }
  ~X() { cout << "-"; }
};

auto *p = new initalizer_list<X>{1, 2, 3}; // ... not at this
delete p; // C++11 requires "now" at this point ...

(again not considering those DRs that revise these rules). 

I think that a warning against "({...})" would be useful too

    // fine
    initializer_list<int> a{1, 2, 3};

    // this is bad
    initializer_list<int> b({1, 2, 3});

Second one is bad because it will destroy the array after initializing 'b', and
won't lengthen the lifetime (because it will use the copy/move constructor).


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]