| Author |
STL and "function try blocks"
|
|
| Babak Pourat 2006-02-23, 7:05 pm |
| 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?
thanks
Babak
| |
| Brian Muth 2006-02-23, 7:05 pm |
| Change to:
int testTry()
{
try{
std::vector<int> *x = new std::vector<int>;
delete x;
return 0;
}
catch (...)
{
return 0;
}
}
| |
| Heinz Ozwirk 2006-02-23, 7:05 pm |
| "Babak Pourat" <pourat@autronic.melchers.de> schrieb im Newsbeitrag
news:466bgpF9hsfbU1@individual.net...
> 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 . 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
| |
| mzdude 2006-02-24, 7:03 pm |
| > 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?
>
> thanks
> Babak
The function testTry() doesn't have a body. You need {}.
Try the following
int TestTry()
{
... // paste your try catch code here
}
int main ....
| |
| Carl Daniel [VC++ MVP] 2006-02-25, 3:57 am |
| mzdude wrote:
>
> The function testTry() doesn't have a body. You need {}.
Actually, no. The original function is valid according to the C++ standard.
It uses what's known as a function try block. Function try blocks have no
value except for in constructors, as the contents of the
constructor-initializer-list is inside the function try block, but outside
the function body.
VC++ apparently has some bugs with function try blocks - but the original
code was valid.
-cd
| |
| Babak Pourat 2006-02-27, 4:10 am |
| Carl Daniel [VC++ MVP] wrote:
> Function try
> blocks have no value except for in constructors, as the contents of
> the constructor-initializer-list is inside the function try block,
> but outside the function body.
I'm not sure, but I expect thay have sence in destructors too. Thay should
catch exceptions in objects with automaticly called destructors(I know,
exceptions in destructors are evil!).
e.g.:
#include <iostream>
class Y{
public:
~Y(){
throw 0;
}
};
class X{
Y y;
public:
~X()try{
}
catch (int &) {
std::cout << "exception catched\n";
}
};
| |
| mzdude 2006-02-27, 7:14 pm |
| I looked in section 8.4 on Function defintions and didn't see that. The
closest I saw was the ctor-initializer which doesn't apply since the
OP's code shows a function and not a class. However, if you can point
me towards the correct section of the standard I will have learned
something new. :^)
| |
| Igor Tandetnik 2006-02-27, 7:14 pm |
| mzdude <jsanga@cox.net> wrote:
> I looked in section 8.4 on Function defintions and didn't see that.
> The closest I saw was the ctor-initializer which doesn't apply since
> the OP's code shows a function and not a class. However, if you can
> point me towards the correct section of the standard I will have
> learned something new. :^)
15/3
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| mzdude 2006-02-28, 7:07 pm |
| 8.4.1 of the standard says functions with function-try-block shall be
defined only in namespace or class scope. Since your example is in
global namespace it fails. Do the following
namespace
{
// your example
}
and everything will work. Both in VC 7 and VC8
| |
| Igor Tandetnik 2006-02-28, 7:07 pm |
| mzdude <jsanga@cox.net> wrote:
> 8.4.1 of the standard says functions with function-try-block shall be
> defined only in namespace or class scope. Since your example is in
> global namespace it fails.
Global namespace _is_ namespace scope. Note that 8.4/1 does not
differentiate between functions with or without try block: "A function
shall be defined only in namespace or class scope." You don't suggest
that all functions must be in an explicitly declared namespace, right?
See 3.3.2 through 3.3.6 for a list of all possible kinds of scope.
> Do the following
>
> namespace
> {
> // your example
> }
>
> and everything will work. Both in VC 7 and VC8
If this is true (I haven't checked), it just means the compiler is
broken in subtler ways than previously thought.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| mzdude 2006-02-28, 9:58 pm |
| Igor Tandetnik wrote:
> mzdude <jsanga@cox.net> wrote:
>
> Global namespace _is_ namespace scope. Note that 8.4/1 does not
> differentiate between functions with or without try block: "A function
> shall be defined only in namespace or class scope." You don't suggest
> that all functions must be in an explicitly declared namespace, right?
> See 3.3.2 through 3.3.6 for a list of all possible kinds of scope.
>
<snip>
IMHO namespace _is_not_ the same as global namespace. If it were the
same, there would be no reason for the standard to explicitly state it.
I'm not a language lawyer, just a programmer in the trenches, but seems
clear to me. (A few more brews and it might even make sense). I'm not
saying functions with try/catch blocks need to be in namespace, but
functions that support function-try-blocks seem to need to be there.
At any rate, I think this is poor programming practice by the OP. If it
is done to explore the nooks and crannies of the C++ language OK. If
done in production code, I would recommend boiling oil.
| |
| Igor Tandetnik 2006-02-28, 9:58 pm |
| "mzdude" <jsanga@cox.net> wrote in message
news:1141180038.695440.121150@t39g2000cwt.googlegroups.com
> Igor Tandetnik wrote:
> <snip>
>
> IMHO namespace _is_not_ the same as global namespace. If it were the
> same, there would be no reason for the standard to explicitly state
> it.
A namespace scope is "top-level" scope - a scope outside any function or
class. Whenever the standard says "namespace scope" that's what it
means. I can't think of any C++ construct that must appear in an
explicitly declared namespace and cannot appear in a global namespace.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Tom Widmer [VC++ MVP] 2006-03-01, 7:59 am |
| mzdude wrote:
> Igor Tandetnik wrote:
>
>
> <snip>
>
> IMHO namespace _is_not_ the same as global namespace.
global scope is a namespace scope - the scope of the global namespace.
See 3.3.5/3.
Tom
|
|
|
|