http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57199
Bug #: 57199 Summary: [4.8, 4.9] Bogus warning: iteration NNNN invokes undefined behavior -Waggressive-loop-optimizations Classification: Unclassified Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: ppluzhni...@google.com Google ref: b/8856275 Test case: typedef decltype(sizeof(0)) size_t; struct Foo { ~Foo(); // comment out -> problem disappears int x[20]; }; template <typename T, int N> struct InlinedVector { inline InlinedVector() : size_(0) { } inline size_t size() const { return size_; } T* mutable_array(); void resize(size_t n) { size_t s = size(); DestroyElements(mutable_array() + n, s - n); } static void DestroyElements(T* ptr, size_t num) { for (size_t i = 0; i < num; i++) { ptr[i].~T(); } } size_t size_; }; struct CP { void Add(); InlinedVector<Foo,1> foo_; }; void CP::Add() { foo_.resize(foo_.size() + 1); } Using trunk r198689: g++ -std=c++11 -O2 -c tt.cc tt.cc: In member function ‘void CP::Add()’: tt.cc:21:7: warning: iteration 230584300921369396ul invokes undefined behavior [-Waggressive-loop-optimizations] ptr[i].~T(); ^ tt.cc:20:5: note: containing loop for (size_t i = 0; i < num; i++) { ^