I'm not compiling with any non-default flags or anything like that. It just seems strange that I'm able to do something this illegal without even a warning. Basically, I guess my question is this : Under which circumstances would this be allowed? Because apparently, it's perfectly fine.
I'm using the VC++2008 express IDE if it makes a difference.
I'm not compiling with any non-default flags or anything like that.
That's probably why -- try upping the warning level in the compiler settings.
With my compiler, I get this:
test.cpp: In function 'int& foo()':
test.cpp:4:6: warning: reference to local variable 'a' returned [-Wreturn-local-addr]
int a = 10;
^
test.cpp: At global scope:
test.cpp:8:5: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char* argv[]) {
^
test.cpp:8:5: warning: unused parameter 'argv' [-Wunused-parameter]
I haven't tried with MSVC++ yet, but I would hope that they would have a similar warning for it.
EDIT: Yeah, it does:
warning C4172: returning address of local variable or temporary
warning C4100: 'argv' : unreferenced formal parameter
warning C4100: 'argc' : unreferenced formal parameter
Ah yes, I'm getting a warning now as well. It's strange nonetheless why I should be able to do this in the first place... Thanks for taking the time long double main!