Without attempting to compile it, is the following code compilable? Is t in scope in the if body or the else?
#include <iostream>
using namespace std;
int returnanint() {
return 1;
}
using namespace std;
int main(int argc, char * argv[])
{
if (int t=returnanint()) {
cout << "t > 0 = " << t << endl;
}
else {
cout << "t==0 " << endl;
t++;
}
return 0;
}
Answer below
.
.
.
.
.
.
.
.
.
.
.
.
.
The answer is, yes it compiles. It's an interesting and certainly not intuitive gray area of C++. The int is declared outside of the braces following the if so is in scope for both those braces and those for the else branch.
No comments:
Post a Comment