[Bug c++/79307] New: g++ misses warning for reference on temporary that invokes undefined behaviour
steven.spark at gmail dot com
gcc-bugzilla@gcc.gnu.org
Tue Jan 31 17:11:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79307
Bug ID: 79307
Summary: g++ misses warning for reference on temporary that
invokes undefined behaviour
Product: gcc
Version: 5.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: steven.spark at gmail dot com
Target Milestone: ---
I thought (something like) this was fixed in bug 986
Code:
struct TestRefInt {
TestRefInt(const int& a) : a_(a) {};
void DoSomething() { cout << "int:" << a_ << endl; }
protected:
const int& a_;
};
TestRefInt tfi(55);
int main() {
TestRefInt ltfi(8);
tfi.DoSomething();
ltfi.DoSomething();
return 0;
}
The first one does not work, the second one happens to work (but only by
"accident").
Similar issue:
struct TestRefIntDirect {
TestRefIntDirect(int a) : a_(a) {};
void DoSomething() { cout << "int:" << a_ << endl; }
protected:
const int& a_;
};
Lifetime of the temporary is not extended in neither case (as per C++11
standard 12.2 p5).
Tested with MinGW 5.3.0, avr-g++ 4.8.1, 4.9.2.
I would like if warning were produced for these. Any help would be appreciated.
I posted more details on stack overflow
http://stackoverflow.com/questions/41928634/c11-scoping-and-lifetime-of-temporary-bound-to-a-const-reference-gcc
Possible duplicate of bug #44859 or bug #51270.
More information about the Gcc-bugs
mailing list