https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974
--- Comment #8 from Szikra <steven.spark at gmail dot com> ---
(In reply to Marc Glisse from comment #7)
> We currently warn on all the examples involving X, with -O2. We don't for Y,
> we might if there was a caller and the dangling reference was used there...
Hi, I assume you mean like this:
#include <iostream>
using namespace std;
struct Y {
Y(int& i) : r(i) { }
int& r;
};
Y f() {
int i=1;
return Y(i);
}
int main() {
Y y1= f();
cout << y1.r << endl; /// warning: 'i' is used uninitialized in this
function [-Wuninitialized]
}
/* compiled with gcc version 6.3.0 (x86_64-posix-seh-rev1, Built by MinGW-W64
project)
g++ -std=c++11 -O2 -g3 -Wall -Wextra -Wconversion -c -fmessage-length=0 -v -o
"src\\test3.o" "..\\src\\test3.cpp"
*/
What about this?
struct Y2 {
Y2(const int& i) : r(i) { }
const int& r;
};
We might or might not get a warning even if the reference is used.
int main() {
Y2 y2(2);
cout << y2.r << endl; /// warning: '<anonymous>' is used uninitialized in
this function [-Wuninitialized]
}
and
Y2 g_y2(22);
int main() {
cout << g_y2.r << endl; /// no warning at all
}
Or what about this?
struct Z {
Z(int i) : r(i) { }
int& r;
};
Z g_z(33);
int main() {
cout << g_z.r << endl; /// no warning
Z z(3);
cout << z.r << endl; /// no warning
}