Re: STL and "function try blocks"



"Babak Pourat" <pourat@xxxxxxxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:466bgpF9hsfbU1@xxxxxxxxxxxxxxxxx
Hallo

the following code does not compile (VC 7.1):

#include <vector>

int testTry() try{
std::vector<int> *x = new std::vector<int>;
delete x;
return 0;
}catch (...) {
return 0;
}

int main(){
return 0;
}


d:\tmp\trytst\trytst\trytst.cpp(11) : warning C4091: '' : ignored on left
of 'int' when no variable is declared
d:\tmp\trytst\trytst\trytst.cpp(11) : error C2143: syntax error : missing
';' before 'inline function header'

Does any one understand why it compiles if I delete the "delete x;"
statement?

No. I can only guess that the compiler is confused. It really looks like a
compiler bug. It is valid code and accepted by other compilers, like Comeau.
But there are two workarounds the problem:

1. Don't use function-try-blocks unless you really have to.
2. Insert a semicolon after the end of the catch (and before int main). This
is not required by the language, and strictly speaking the program will be
no valid C++ any longer, but if the compiler likes it better.

Perhaps there is also

3. Upgrade to VC8, but I don't know if the bug has been fixed.

HTH
Heinz


.