My course notes say “C++ requires declaration before use in a block and among types but not within a type.”
Is this what it means?
int f() {
if (i)
return i;
int i = 1; //allowed?
return 0;
}
//not allowed?
int g() {
if (i)
return i;
return 0;
}
int i = 1;
No. Both your examples are “in a block” and neither of those is allowed. If you try compiling your example code you’ll get an error immediately.
However, this would be allowed:
This is within a type and that’s the important distinction.