Possible Duplicate:
Why can't variables defined in a conditional be constructed with arguments?
Consider this simple example:
/*1*/ int main() {
/*2*/ for (int i(7); i;){break;}
/*3*/ if (int i(7)) {}
/*4*/ }
Why line-2 compiles just fine, whilst line-3 gives the error? This is little strange to me why if-statement is in this aspect treated worse than for-loop?
If this is compiler specific – I tested with gcc-4.5.1:
prog.cpp: In function ‘int main()’:
prog.cpp:3:7: error: expected primary-expression before ‘int’
prog.cpp:3:7: error: expected ‘)’ before ‘int’
I was inspired by this question
[UPDATE]
I know this compiles just fine:
/*1*/ int main() {
/*2*/ for (int i = 7; i;){break;}
/*3*/ if (int i = 7) {}
/*4*/ }
[UPDATE2]
It seems to be purely academic question – but this could be extremely important for such types as std::unique_ptr<> which cannot be copied:
#include <memory>
int main() {
if (std::unique_ptr<int> i = new (std::nothrow) int(7)) {
}
if (std::unique_ptr<int> i(new (std::nothrow) int(7))) {
}
}
Neither of these two kinds are allowed. Not sure about new C++11 syntax {}?
The C++ standard doesn’t provide a rationale but I would suspect that using the constructor notation could cause some inconsistencies. For example, since function declarations aren’t allowed in the
ifstatement, the most vexing parse would actually mean what was intended. For example:In C++ 2011 you can use brace-initialization:
Unfortunately, brace-initialization doesn’t always mean the same thing as using constructor notation (I think it’s called direct-initialization).
With respect to the update: you can use one of these:
It seems there are plenty of options 🙂